dom4j解析xml
答案:2 悬赏:10 手机版
解决时间 2021-11-19 01:32
- 提问者网友:嗝是迷路的屁
- 2021-11-18 13:14
dom4j解析xml
最佳答案
- 五星知识达人网友:迷人又混蛋
- 2021-11-18 13:43
import java.io.File;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class XmlTest2 {
public static void main(String[] args) {
SAXReader reader = new SAXReader();
reader.setEncoding("UTF-8");
Document doc = null;
try {
File file = new File("test2.xml");
doc = reader.read(file);
printNode(doc.selectNodes("DSExport/Job"));
}
catch (DocumentException e) {
e.printStackTrace();
}
}
public static void printNode(List nodeList) {
for (Node jobNode : nodeList) {
printProperty(jobNode.selectNodes("Record/Property"));
printProperty(jobNode.selectNodes("Record/Collection/SubRecord/Property"));
}
}
public static void printProperty(List propertyNodeList) {
for (Node propertyNode : propertyNodeList) {
System.out.println(propertyNode.getUniquePath() + ">>" + propertyNode.selectSingleNode("@Name").getText() + ":" + propertyNode.getText());
}
}
}
追问还有个问题请教下,如果是比较大的xml文件大概300M左右大小,使用ElementHandler来解析的话该如何去写呢?追答就是实现ElementHandler接口,在onEnd的时候取节点元素的信息。
import java.io.File;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.ElementHandler;
import org.dom4j.ElementPath;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class XmlTest2 {
public static void main(String[] args) {
SAXReader reader = new SAXReader();
reader.setEncoding("UTF-8");
Document doc = null;
try {
File file = new File("test2.xml");
reader.addHandler("/DSExport/Job", new MyElementHandle());
doc = reader.read(file);
// printNode(doc.selectNodes("DSExport/Job"));
} catch (DocumentException e) {
e.printStackTrace();
}
}
public static void printNode(List nodeList) {
for (Node jobNode : nodeList) {
printProperty(jobNode.selectNodes("Record/Property"));
printProperty(jobNode.selectNodes("Record/Collection/SubRecord/Property"));
}
}
public static void printProperty(List propertyNodeList) {
for (Node propertyNode : propertyNodeList) {
System.out.println(propertyNode.getUniquePath() + ">>" + propertyNode.selectSingleNode("@Name").getText() + ":" + propertyNode.getText());
}
}
}
class MyElementHandle implements ElementHandler {
@Override
public void onStart(ElementPath path) {}
@Override
public void onEnd(ElementPath path) {
Element row = path.getCurrent();
XmlTest2.printProperty(row.selectNodes("Record/Property"));
XmlTest2.printProperty(row.selectNodes("Record/Collection/SubRecord/Property"));
row.detach();
}
}上面注意我实现了ElementHandler接口的类MyElementHandler。
BTW,希望你能多看看api,自己也能轻易找到解决办法的。
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class XmlTest2 {
public static void main(String[] args) {
SAXReader reader = new SAXReader();
reader.setEncoding("UTF-8");
Document doc = null;
try {
File file = new File("test2.xml");
doc = reader.read(file);
printNode(doc.selectNodes("DSExport/Job"));
}
catch (DocumentException e) {
e.printStackTrace();
}
}
public static void printNode(List
for (Node jobNode : nodeList) {
printProperty(jobNode.selectNodes("Record/Property"));
printProperty(jobNode.selectNodes("Record/Collection/SubRecord/Property"));
}
}
public static void printProperty(List
for (Node propertyNode : propertyNodeList) {
System.out.println(propertyNode.getUniquePath() + ">>" + propertyNode.selectSingleNode("@Name").getText() + ":" + propertyNode.getText());
}
}
}
追问还有个问题请教下,如果是比较大的xml文件大概300M左右大小,使用ElementHandler来解析的话该如何去写呢?追答就是实现ElementHandler接口,在onEnd的时候取节点元素的信息。
import java.io.File;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.ElementHandler;
import org.dom4j.ElementPath;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class XmlTest2 {
public static void main(String[] args) {
SAXReader reader = new SAXReader();
reader.setEncoding("UTF-8");
Document doc = null;
try {
File file = new File("test2.xml");
reader.addHandler("/DSExport/Job", new MyElementHandle());
doc = reader.read(file);
// printNode(doc.selectNodes("DSExport/Job"));
} catch (DocumentException e) {
e.printStackTrace();
}
}
public static void printNode(List
for (Node jobNode : nodeList) {
printProperty(jobNode.selectNodes("Record/Property"));
printProperty(jobNode.selectNodes("Record/Collection/SubRecord/Property"));
}
}
public static void printProperty(List
for (Node propertyNode : propertyNodeList) {
System.out.println(propertyNode.getUniquePath() + ">>" + propertyNode.selectSingleNode("@Name").getText() + ":" + propertyNode.getText());
}
}
}
class MyElementHandle implements ElementHandler {
@Override
public void onStart(ElementPath path) {}
@Override
public void onEnd(ElementPath path) {
Element row = path.getCurrent();
XmlTest2.printProperty(row.selectNodes("Record/Property"));
XmlTest2.printProperty(row.selectNodes("Record/Collection/SubRecord/Property"));
row.detach();
}
}上面注意我实现了ElementHandler接口的类MyElementHandler。
BTW,希望你能多看看api,自己也能轻易找到解决办法的。
全部回答
- 1楼网友:忘川信使
- 2021-11-18 14:08
mnnjnjnjjnjnjn
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯