2016-08-26 4 views
0

dom4j로 XML을 수정하려고합니다.
dom4j에는 검색된만큼 충분한 메소드 (예 : getNextSibling, hasAttribute)가 없는데
DOMElement 클래스는이 프로그렘을 해결하는 데 도움이됩니다.
하지만 DOMElement 클래스를 사용하는 방법을 이해할 수 없습니다 ...dom4j 문서의 DOMElement 클래스 사용 방법

DOMElement를 사용하여 아래 코드를 다시 작성하는 방법 ??
http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/dom/DOMElement.html

import java.util.Iterator; 
import java.util.List; 

import org.dom4j.Document; 
import org.dom4j.DocumentException; 
import org.dom4j.Node; 
import org.dom4j.io.SAXReader; 

public class Sampe1 { 

public static void main(String[] args) { 
    SAXReader reader = new SAXReader(); 

    try { 
     Document document = reader.read("catalog.xml"); 

     List books =document.selectNodes("catalog/book"); 
     for(Iterator i = books.iterator(); i.hasNext();){ 
      Node book = (Node)i.next(); 
      List aList = book.selectNodes("./a"); 
      for(Iterator i2 = aList.iterator(); i2.hasNext();){ 
       Node aNode = (Node)i2.next(); 
       int aInt = Integer.parseInt(aNode.valueOf("./@volume")); 


       **// I want to rewrite the next line By "getNextSibling"method of DOMElement(dom4j.dom)** 
       Node NextSiblingNode= aNode.selectSingleNode("./following-sibling::*"); 
       int nextInt = Integer.parseInt(NextSiblingNode.valueOf("./@volume")); 



**//I want to rewrite the following lines By "hasAttribute"method of DOMElement** 
//     if(aNode.hasAttribute("volume");){ 
//      if(NextSiblingNode.hasAttribute("volume")){ 
//       if(aInt > nextInt){ 
//        System.out.println(aInt+"is larger than"+ nextInt); 
//       } 
//      } 
//     } 




      } 



    } 


} 
      catch(DocumentException e) 
      { 
      System.out.println(e.getMessage()); 
         } 

} 
} 

catalog.xml

enter code here 

<?xml version="1.0"?> 
<catalog> 
    <book> 
    <a num = "1" volume = "10">Gambardella, Matthew</a> 
    <a num = "2" volume = "7">XML Developer's Guide</a> 
    <a num = "3" volume = "3">Computer</a> 
    <a num = "4">44.95</a> 
    <a num = "5">2000-10-01</a> 
    <a num = "6">An in-depth look at creating applications 
    with XML.</a> 
    </book> 
    <book> 
    <a num = "3" volume = "3">Fantasy</a> 
    <a num ="4" >5.95</a> 
    <a num = "5">2000-12-16</a> 
    <a num = "6">A former architect battles corporate zombies, 
    an evil sorceress, and her own childhood to become queen 
    of the world.</a> 
    </book> 
    <book> 
    <a num = "4">5.95</a> 
    <a num = "5">2000-11-17</a> 
    <a num = "6">After the collapse of a nanotechnology 
    society in England, the young survivors lay the 
    foundation for a new society.</a> 
    </book> 
</catalog> 

답변

0

당신은 Element에 XML의 속성을 다음 속성 작업 Node 개체를 캐스팅 할 수 있습니다.

package test; 

import java.util.Iterator; 
import java.util.List; 

import org.dom4j.Attribute; 
import org.dom4j.Document; 
import org.dom4j.DocumentException; 
import org.dom4j.Element; 
import org.dom4j.Node; 
import org.dom4j.io.SAXReader; 

public class TestMain { 

    public static void main(String[] args) { 
     SAXReader reader = new SAXReader(); 

     try { 
      Document document = reader.read("catalog.xml"); 

      List books = document.selectNodes("catalog/book"); 
      for (Iterator i = books.iterator(); i.hasNext();) { 
       Node book = (Node) i.next(); 
       List<Node> aList = book.selectNodes("./a"); 
       for (Iterator<Node> i2 = aList.iterator(); i2.hasNext();) { 
        Element aNode = (Element) i2.next(); 
        Attribute aNodeVolume = aNode.attribute("volume"); 
        if (aNodeVolume != null) { 
         int aInt = Integer.parseInt(aNodeVolume.getValue()); 

         // I want to rewrite the next line By 
         // "getNextSibling"method 
         // of DOMElement(dom4j.dom)** 
         Element NextSiblingNode = (Element) aNode.selectSingleNode("./following-sibling::*"); 

         // I want to rewrite the following lines By 
         // "hasAttribute"method of DOMElement** 
         if (aNode.attributeValue("volume") != null) { 
          Attribute NextSiblingNodeVolume = NextSiblingNode.attribute("volume"); 
          if (NextSiblingNodeVolume != null) { 
           int nextInt = Integer.parseInt(NextSiblingNodeVolume.getValue()); 
           if (aInt > nextInt) { 
            System.out.println(aInt + "is larger than" + nextInt); 
           } 
          } 
         } 

        } 

       } 
      } 

     } catch (DocumentException e) { 
      System.out.println(e.getMessage()); 
     } 

    } 

} 
0

당신은 SAXReader를 만들기위한 org.dom4j.dom.DOMDocumentFactory를 지정해야 DOMElement을 사용하려면 다음은 프로그램의 작업 버전입니다. DOMDocument이 생성되고 해당 개체를 W3C 또는 org.dom4j.dom.* 클래스로 대소 문자를 구분할 수 있습니다.

import java.util.*; 
import org.dom4j.dom.*; 
import org.dom4j.io.SAXReader; 
import org.w3c.dom.*; 

public class Sampe1 { 

public static void main(String[] args) throws Exception { 
SAXReader reader = new SAXReader(DOMDocumentFactory.getInstance()); 
DOMDocument document = (DOMDocument)reader.read("catalog.xml"); 
List books =document.selectNodes("catalog/book"); 
for(Iterator i = books.iterator(); i.hasNext();){ 
    DOMElement book = (DOMElement)i.next(); 
    List aList = book.elements("a"); 
    for(Iterator i2 = aList.iterator(); i2.hasNext();){ 
     DOMElement aNode = (DOMElement)i2.next(); 
     Node NextSiblingNode = (Node)aNode.getNextSibling(); 
     System.out.println(NextSiblingNode);//next one is org.dom4j.dom.DOMText... 
    } 
} 
} 
+0

감사합니다! 이것은 내가 원하는 바로 그 대답이다. 며칠 동안 내 오류를 해결하려고 노력하고 있습니다. 질문이 있습니다. NextSibling을 Node로 가져오고 싶습니다. 그것을하는 방법 ?? DOMElement 클래스가 그것을 가능하게 할 수 없습니까 ?? 가능한 경우 알려 주실 수 있습니까? – nozomi

+0

org.w3c.dom.Nodeorg.dom4j.Node을 동시에 가져올 수 없으므로 샘플 코드에 import 문을 추가하기 만하면됩니다. 이것이 당신이 원하는 것인지 확인하십시오. –

+0

예를 들어, Iterator를 사용하여 childs 노드로 self에 nextsibling을 추가하는 것이 좋습니다. 감사. – nozomi