1

I have the XML as follows.

            <issn>0040-6031</issn>
            <volume-issue-number>
                <vol-first>630</vol-first>
                <suppl>C</suppl>
            </volume-issue-number> 
            <collection-title>Thermochimica Acta Ila Modified on 7</collection-title>

**

  • Elements issn and collection-title is repeating several times in my xml file.
  • I have to find the collection-title value for the given issn input
  • Exactly need to get the second sibling element collection-title value to the given element issn.
  • But now I can only able to reach the element.
  • below i have provided my code .can any one please help me achieve this?
  • Thanks in advance.

Code Snippet

    doc.getDocumentElement().normalize();
     NodeList nList2=doc.getElementsByTagName("issn");
     if(nList2.getLength()>=1)
     {
     for (int temp2 = 0; temp2 < nList2.getLength(); temp2++) {
            Node nNode4 = nList2.item(temp2);

            if (nNode4.getNodeType() == Node.ELEMENT_NODE) 
            {
               Element eElement1 = (Element) nNode4;
               issn_value[temp2]=eElement1.getTextContent();
               if(issn_value[temp2].equalsIgnoreCase(issn_value_DB))
               {
                System.out.println("Inside issn value comparision:XXXXX");
                System.out.println("equal issn found..");
                System.out.println("value : "+issn_value[temp2]);
                System.out.println("issn value from DB :"+issn_value_DB);
                Node sibling = eElement1.getNextSibling();
                while (!(sibling instanceof Element) && sibling !=null) {
                      sibling = sibling.getNextSibling();
                      System.out.println(" Get the Name of the Next Sibling "+ sibling.getNodeName());
                      System.out.println(" Get the Value of the Next Sibling "+ sibling.getTextContent());
                    }

               }
            }
     }
    }
Pulkit
  • 3,953
  • 6
  • 31
  • 55

2 Answers2

1

This could be tremendously simplified if you can use XPath :

//issn[.='0040-6031']/following-sibling::collection-title[1]

The XPath finds <issn> element which content equals '0040-6031' first, and then return the nearest following sibling <collection-title>.

I don't use Java, but the following thread might help : How to read XML using XPath in Java

Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137
0

I found the below code and works fine.

doc.getDocumentElement().normalize();

  NodeList nList2=doc.getElementsByTagName("issn");
  if(nList2.getLength()>=1)
  {
         for (int temp2 = 0; temp2 < nList2.getLength(); temp2++) {
             Node nNode4 = nList2.item(temp2);

             if (nNode4.getNodeType() == Node.ELEMENT_NODE) 
             {
                Element eElement1 = (Element) nNode4;
                issn_value[temp2]=eElement1.getTextContent();
                if(issn_value[temp2].equalsIgnoreCase(issn_value_DB))
                {
                    System.out.println("Inside issn value 
comparision:XXXXX");
                    System.out.println("equal issn found..");
                    System.out.println("value : "+issn_value[temp2]);
                    System.out.println("issn value from DB : 
"+issn_value_DB);


                    Node childNode = eElement1.getNextSibling();     
                    while( childNode.getNextSibling()!=null ){          
                        childNode = childNode.getNextSibling();         
                        if (childNode.getNodeType() == 
  Node.ELEMENT_NODE) {         
                            Element childElement = (Element) childNode;

  if(childElement.getNodeName().equalsIgnoreCase("collection-title"))
                            {

            title_from_XML=childElement.getTextContent();

    System.out.println("Child node name : "+childElement.getNodeName());
    System.out.println("Child node valueBBBBB:" +title_from_XML );
                            }
                            else
                            {
                                System.out.println("No value found for 
 the element collection-title ");
                            }
                        }       
                    }
 }