Link to home
Start Free TrialLog in
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;
    }
ASKER CERTIFIED SOLUTION
Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rajan416
rajan416

ASKER

Thanks.. I used the second option