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>
감사합니다! 이것은 내가 원하는 바로 그 대답이다. 며칠 동안 내 오류를 해결하려고 노력하고 있습니다. 질문이 있습니다. NextSibling을 Node로 가져오고 싶습니다. 그것을하는 방법 ?? DOMElement 클래스가 그것을 가능하게 할 수 없습니까 ?? 가능한 경우 알려 주실 수 있습니까? – nozomi
org.w3c.dom.Node
과org.dom4j.Node
을 동시에 가져올 수 없으므로 샘플 코드에 import 문을 추가하기 만하면됩니다. 이것이 당신이 원하는 것인지 확인하십시오. –예를 들어, Iterator를 사용하여 childs 노드로 self에 nextsibling을 추가하는 것이 좋습니다. 감사. – nozomi