Ilavarasan Jayaraman
Ilavarasan Jayaraman

Reputation: 303

How to get the second sibling value of an XML element using DOM JAVA?

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>

**

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());
                    }

               }
            }
     }
    }

Upvotes: 1

Views: 1523

Answers (2)

Ilavarasan Jayaraman
Ilavarasan Jayaraman

Reputation: 303

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 ");
                            }
                        }       
                    }
 }

Upvotes: 0

har07
har07

Reputation: 89285

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

Upvotes: 1

Related Questions