Avatar of rajan416
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</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;
    }
JavaXML

Avatar of undefined
Last Comment
rajan416

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
gurpsbassi

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

ASKER
Thanks.. I used the second option
Your help has saved me hundreds of hours of internet surfing.
fblack61