rajan416
asked on
How to pull a value from child element from xml using java method?
I tried to pull the following data from "value" tag to form a list. I am not sure how to get the child element values from xml.
I need some assistance on this
000027
000028
000029
public ArrayList getListofValues()
{
String xml = "<parameters> " +
" <parameter dpId=\"DP0\" type=\"prompt\" optional=\"false\">" +
" <id>2</id>" +
" <technicalName>pmCA ID</technicalName>" +
" <name>CA ID</name>" +
" <answer type=\"Text\" constrained=\"false\">" +
" <info cardinality=\"Multiple\">" +
" <lov refreshable=\"true\" partial=\"true\" hierarchical=\"false\">" +
" <id>UNIVERSELOV_DS0.DO16</ id>" +
" <updated>1969-12-31T16:00: 00.000-08: 00</update d>" +
" <values>" +
" <value>000027</value>" +
" <value>000028</value>" +
" <value>000029</value>" +
" </values>" +
" <columns mappingId=\"0\">" +
" <column type=\"String\" id=\"0\">CA ID </column>" +
" </columns>" +
" </lov>" +
" </info>" +
" " +
" </answer>" +
" </parameter>" ;
ArrayList result=new ArrayList();
try{
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder ();
Document document = builder.parse(new InputSource(new StringReader(xml)));
Element element = document.getDocumentElemen t();
NodeList list = element.getElementsByTagNa me("values ");
if (list != null && list.getLength() > 0) {
NodeList childList = null;
logger.debug("vlist.getLen gth()" + list.getLength());
for (int i = 0; i < list.getLength(); i++) {
System.out.println("Parent name>>>"+list.item(i).getP arentNode( ).getNodeN ame());
if(list.item(i).getParentN ode().getN odeName(). toString() .equalsIgn oreCase("l ov"))
{
childList = list.item(i).getChildNodes ();
childList.item(1).toString ()
for (int j = 0; j < childList.getLength(); j++) {
logger.debug("value before null check>>>>>>>>>>>>>"+ childList.item(j).getNodeV alue());
if(childList.item(j).getNo deValue()! =null && !childList.item(j).getNode Value().is Empty() )
{
logger.debug("value:"+j +":"+ childList.item(j).getNodeV alue());
result.add(childList.item( j).getNode Value());
}
}
}
}
}
}
catch(Exception e)
{
System.out.println("EXCEPT ION"+e);
}
return result;
}
I need some assistance on this
000027
000028
000029
public ArrayList getListofValues()
{
String xml = "<parameters> " +
" <parameter dpId=\"DP0\" type=\"prompt\" optional=\"false\">" +
" <id>2</id>" +
" <technicalName>pmCA ID</technicalName>" +
" <name>CA ID</name>" +
" <answer type=\"Text\" constrained=\"false\">" +
" <info cardinality=\"Multiple\">"
" <lov refreshable=\"true\" partial=\"true\" hierarchical=\"false\">" +
" <id>UNIVERSELOV_DS0.DO16</
" <updated>1969-12-31T16:00:
" <values>" +
" <value>000027</value>" +
" <value>000028</value>" +
" <value>000029</value>" +
" </values>" +
" <columns mappingId=\"0\">" +
" <column type=\"String\" id=\"0\">CA ID </column>" +
" </columns>" +
" </lov>" +
" </info>" +
" " +
" </answer>" +
" </parameter>" ;
ArrayList result=new ArrayList();
try{
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder
Document document = builder.parse(new InputSource(new StringReader(xml)));
Element element = document.getDocumentElemen
NodeList list = element.getElementsByTagNa
if (list != null && list.getLength() > 0) {
NodeList childList = null;
logger.debug("vlist.getLen
for (int i = 0; i < list.getLength(); i++) {
System.out.println("Parent
if(list.item(i).getParentN
{
childList = list.item(i).getChildNodes
childList.item(1).toString
for (int j = 0; j < childList.getLength(); j++) {
logger.debug("value before null check>>>>>>>>>>>>>"+ childList.item(j).getNodeV
if(childList.item(j).getNo
{
logger.debug("value:"+j +":"+ childList.item(j).getNodeV
result.add(childList.item(
}
}
}
}
}
}
catch(Exception e)
{
System.out.println("EXCEPT
}
return result;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER