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

时间:2016-05-07 06:35:01

标签: java xml parsing dom

我的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输入的集合标题值
  • 确实需要将第二个兄弟元素集合标题值添加到给定元素中。
  • 但现在我只能到达元素了。
  • 下面我提供了我的代码。可以帮助我实现这个吗?
  • 提前致谢。

代码段

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

               }
            }
     }
    }

2 个答案:

答案 0 :(得分:0)

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

<ul>
   <li *ngFor="#item of items; #last = last">
    <span *ngIf='last'>{{hello(last)}}</span>
    {{item}}
   </li>
  </ul>


items: Array<number> = [1,2,3,4,5]
  constructor() { console.clear();}
  hello(a){
    if(a==true)
      this.callbackFunction();
  }
  callbackFunction(){
    console.log("last element");
  }

XPath首先找到内容等于'0040-6031'的//issn[.='0040-6031']/following-sibling::collection-title[1] 元素,然后返回最近的兄弟<issn>

我不使用Java,但以下主题可能有所帮助:How to read XML using XPath in Java

答案 1 :(得分:0)

我找到了以下代码并且工作正常。

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