About
Pricing
Community
Teams
Start Free Trial
Log in
rajan416
asked on
4/5/2015
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;
}
Java
XML
2
1
Last Comment
rajan416
8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
gurpsbassi
4/6/2015
THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
rajan416
4/7/2015
ASKER
Thanks.. I used the second option
Your help has saved me hundreds of hours of internet surfing.
fblack61