如何使用DOM JAVA获取XML元素的第二个同级值?

如何使用DOM JAVA获取XML元素的第二个同级值?,java,xml,parsing,dom,Java,Xml,Parsing,Dom,我的XML如下所示 <issn>0040-6031</issn> <volume-issue-number> <vol-first>630</vol-first> <suppl>C</suppl> </volume-issue-number>

我的XML如下所示

            <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>
0040-6031
630
C
7月7日修改的Thermochimica Acta Ila
**

  • 元素issn和集合标题在我的xml文件中重复了好几次
  • 我必须找到给定issn输入的集合标题值
  • 需要获取给定元素issn的第二个同级元素集合标题值
  • 但现在我只能到达元素
  • 下面我已经提供了我的代码。有人能帮我实现吗
  • 提前谢谢
代码片段
doc.getDocumentElement().normalize();
NodeList nList2=doc.getElementsByTagName(“issn”);
如果(nList2.getLength()>=1)
{
for(int temp2=0;temp2
如果您可以使用XPath,这可以大大简化:

//issn[.='0040-6031']/following-sibling::collection-title[1]
XPath首先查找内容等于“0040-6031”的
元素,然后返回最近的后续同级


我不使用Java,但下面的线程可能会有所帮助:

如果您可以使用XPath,这可以大大简化:

//issn[.='0040-6031']/following-sibling::collection-title[1]
XPath首先查找内容等于“0040-6031”的
元素,然后返回最近的后续同级


我不使用Java,但下面的线程可能会有所帮助:

我找到了下面的代码,工作正常

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 ");
                            }
                        }       
                    }
 }
doc.getDocumentElement().normalize();
NodeList nList2=doc.getElementsByTagName(“issn”);
如果(nList2.getLength()>=1)
{
for(int temp2=0;temp2
我找到了下面的代码,工作正常

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 ");
                            }
                        }       
                    }
 }
doc.getDocumentElement().normalize();
NodeList nList2=doc.getElementsByTagName(“issn”);
如果(nList2.getLength()>=1)
{
for(int temp2=0;temp2