簡體   English   中英

如何使用DOM JAVA獲取XML元素的第二個兄弟值?

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

我有如下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>

**

  • 元素issn和collection-title在我的xml文件中重復多次。
  • 我必須找到給定的issn輸入的集合標題值
  • 完全需要將第二個兄弟元素集合標題值賦予給定元素issn。
  • 但現在我只能到達元素了。
  • 下面我提供了我的代碼。可以任何人幫我實現這個目標嗎?
  • 提前致謝。

代碼片段

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

               }
            }
     }
    }

如果你可以使用XPath,這可以大大簡化:

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

XPath首先找到<issn>元素,其內容等於'0040-6031',然后返回最近的兄弟<collection-title>

我不使用Java,但以下線程可能有所幫助: 如何使用Java中的XPath讀取XML

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM