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</updated>" +
                  "                    <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.getDocumentElement();
                NodeList list = element.getElementsByTagName("values");

                if (list != null && list.getLength() > 0) {
                      NodeList childList = null;
                      logger.debug("vlist.getLength()" + list.getLength());
                      for (int i = 0; i < list.getLength(); i++) {
                            System.out.println("Parent name>>>"+list.item(i).getParentNode().getNodeName());
                            if(list.item(i).getParentNode().getNodeName().toString().equalsIgnoreCase("lov"))
                            {
                                  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).getNodeValue());
                                        if(childList.item(j).getNodeValue()!=null && !childList.item(j).getNodeValue().isEmpty() )
                                        {
                                              logger.debug("value:"+j +":"+ childList.item(j).getNodeValue());
                                            result.add(childList.item(j).getNodeValue());
                                        }
                                  }
                                  
                            }
                      }
                }
          }
          catch(Exception e)
          {
                System.out.println("EXCEPTION"+e);
          }
          return result;
    }
rajan416Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gurpsbassiCommented:
First thing: your xml is missing a </parameters> tag. Without this, you won't be able to parse it.

I am assuming you are using Java 1.5 or above. In which case you should make use of generics i.e
ArrayList result = new ArrayList() should be declared as ArrayList<String> result = new ArrayList<String>().

In Java 1.5 and above your code can easily be reduced to simply a few lines as below:
XPathFactory xpf = XPathFactory.newInstance();
	                XPath xp = xpf.newXPath();
	                XPathExpression xpe = xp.compile("//lov/values/value/text()");
	                
	                NodeList nodes = (NodeList)xpe.evaluate(document , XPathConstants.NODESET);
	                for(int i = 0; i < nodes.getLength(); i ++){
	                	result.add(nodes.item(i).getNodeValue());
	                }

Open in new window


You simply use XPath to get the text nodes you want without having to go back up the tree to find parents e.t.c. The beauty of the XPath expression is that it will only find the <value> tags that have got a text value. So you don't need to check if the value is populated i.e. tags like <value></value> will be excluded from the search.

If you're not using Java 1.5+ and still want to do it in your original fashion then I would suggest changing the if statement to the following:
if(childList.item(j).getFirstChild() !=null && !childList.item(j).getFirstChild().getNodeValue().isEmpty() )
	                                        {
	                                        	logger.debug("value:"+j +":"+ childList.item(j).getFirstChild().getNodeValue());
	                                            result.add(childList.item(j).getFirstChild().getNodeValue());
	                                        }

Open in new window

 I have made use of getFirstChild() here because calling getNodeValue() on any other node than a text node will not give you back the text. Remember the content inside <value></value> is also another node of type text. See API doc: Java doc

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rajan416Author Commented:
Thanks.. I used the second option
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.