Ilavarasan Jayaraman Ilavarasan Jayaraman - 6 months ago 16
Java Question

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>


**


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

}
}
}
}

Answer

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