dom4j 로 XML 분석 하기
<?xml version="1.0" encoding="UTF-8"?>
<books>
<xiyouji id="x001">
<author> </author>
<times> </times>
</xiyouji>
<hongloumeng id="x002">
<author> </author>
</hongloumeng>
</books> 2. 테스트 클래스package com.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
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.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class TestDOM4JForXML {
public static void main(String[] args) throws Exception {
TestDOM4JForXML.readXML("dom4j.xml");
TestDOM4JForXML.updateXML("dom4j.xml");
}
/**
* document
*/
public static Document getDocument(String fileName) {
SAXReader saxReader = new SAXReader();
Document document = null;
try {
document = saxReader.read(new File(fileName));// read , Document
} catch (DocumentException e) {
e.printStackTrace();
}
return document;
}
/**
* xml
*/
public static void readXML(String fileName) throws Exception {
Document document = getDocument(fileName);
Element node = document.getRootElement(); //
listNodes(node);
}
public static void listNodes(Element node) {
System.out.println(" :" + node.getName()); //
List<Attribute> list = node.attributes(); //
for (Attribute attr : list) { //
System.out.println(attr.getText() + "-----" + attr.getName() + "---" + attr.getValue());
}
if (!(node.getTextTrim().equals(""))) {
System.out.println(" :" + node.getText());
}
Iterator<Element> it = node.elementIterator(); //
while (it.hasNext()) {
Element e = it.next(); //
listNodes(e); //
}
}
/**
* xml
*/
public static void updateXML(String fileName) throws Exception {
Document document = getDocument(fileName);
Element node = document.getRootElement(); //
Element element = node.element("hongloumeng"); // books , hongloumeng 。
Attribute attr = element.attribute("id"); // element id
element.remove(attr); //
element.addAttribute("name", " "); //
Element newElement = element.addElement(" "); // hongloumeng
newElement.setText(" "); // element
Element author = element.element(" ");
boolean flag = element.remove(author); // ( true , )
Element descElement = element.addElement(" ");
descElement.addCDATA(" , 。"); // CDATA
writer(document);
}
/**
* document
*/
public static void writer(Document document) throws Exception {
// OutputFormat format = OutputFormat.createCompactFormat(); //
OutputFormat format = OutputFormat.createPrettyPrint(); //
format.setEncoding("UTF-8"); //
// XMLWriter ,
// XMLWriter writer = new XMLWriter(new FileWriter(new File("dom4j.xml")),format);
XMLWriter writer = new XMLWriter(new OutputStreamWriter( new FileOutputStream(new File("dom4j.xml")), "UTF-8"), format);
writer.write(document); //
writer.flush(); //
writer.close(); //
}
} 3. 인 스 턴 스 (문자열 과 XML 의 상호 변환)
package com.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class TestParseXML {
public static void main(String[] args) throws Exception {
TestParseXML.parseXML();
TestParseXML.updateXML();
TestParseXML.readXML();
}
/**
* document
*/
public static Document getDocument(String fileName) {
SAXReader saxReader = new SAXReader();
Document document = null;
try {
document = saxReader.read(new File(fileName));// read , Document
} catch (DocumentException e) {
e.printStackTrace();
}
return document;
}
/**
* Document
*/
public static void parseXML() throws Exception {
String text = "<csdn><java>Java </java><net>Net </net></csdn>";
Document document = DocumentHelper.parseText(text);
writer(document);
}
/**
* document document xml
*/
public static void updateXML() throws Exception {
Document document = DocumentHelper.createDocument(); //
Element root = document.addElement("csdn");
Element java = root.addElement("java");
java.setText("java ");
Element ios = root.addElement("ios");
ios.setText("ios ");
writer(document);
}
public static void readXML() {
Document document = getDocument("csdn.xml");
Element root = document.getRootElement();
String docXmlText = document.asXML(); //
System.out.println(docXmlText);
System.out.println("---------------------------");
String rootXmlText = root.asXML(); // books
System.out.println(rootXmlText);
System.out.println("--------------------------");
Element e = root.element("java"); // hongloumeng
System.out.println(e.asXML());
}
/**
* document
*/
public static void writer(Document document) throws Exception {
// OutputFormat format = OutputFormat.createCompactFormat(); //
OutputFormat format = OutputFormat.createPrettyPrint(); //
format.setEncoding("UTF-8"); //
// XMLWriter ,
// XMLWriter writer = new XMLWriter(new FileWriter(new File("csdn.xml")),format);
XMLWriter writer = new XMLWriter(new OutputStreamWriter( new FileOutputStream(new File("csdn.xml")), "UTF-8"), format);
writer.write(document); //
writer.flush(); //
writer.close(); //
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.