码迷,mamicode.com
首页 > 其他好文 > 详细

解析xml:dom,sax,jdom,dom4j,推荐dom4j

时间:2015-07-28 10:39:17      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

1):dom解析,放在内存中占用内存比较大

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document d = db.parse("src/student.xml");
NodeList nl = d.getElementsByTagName("student");
Element e = (Element) nl.item(0);
System.out.println(e.getElementsByTagName("name").item(0)
.getFirstChild().getNodeValue());
System.out.println(e.getElementsByTagName("sex").item(0)
.getFirstChild().getNodeValue());
} catch (Exception e) {
e.printStackTrace();
}

2)sax解析,不能修改文件,操作比较复杂

public class SaxTest extends DefaultHandler {

public static void main(String[] args) {
SAXParserFactory spf = SAXParserFactory.newInstance();
try {
SAXParser sp = spf.newSAXParser();
sp.parse("src/students.xml", new SaxTest());
} catch (Exception e) {
e.printStackTrace();
}

}

@Override
public void endDocument() throws SAXException {
System.out.println("扫描文档结束");
}

@Override
public void startDocument() throws SAXException {
System.out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
}

@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
System.out.println("扫描节点开始" + qName);
}

@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
System.out.println("扫描文本节点" + new String(ch, start, length));
}

@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
System.out.println("扫描节点结束" + qName);
}

}

3)jdom解析

   1:创建xml

Element e = new Element("student");
Attribute id = new Attribute("id", "100");
Attribute aa = new Attribute("aa", "AA");

e.setAttribute(id);
e.setAttribute(aa);

Element name = new Element("name");
name.setText("zhangsan");
e.addContent(name);
Element sex = new Element("sex");
sex.setText("male");
e.addContent(sex);
Element age = new Element("age");
age.setText("20");
e.addContent(age);

Document d = new Document(e);
XMLOutputter xop = new XMLOutputter();
xop.setFormat(xop.getFormat().setEncoding("UTF-8"));

try {
xop.output(d, new FileOutputStream("src/student.xml"));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}

   2:解析xml

SAXBuilder sb = new SAXBuilder();
Document d = sb.build("src/students.xml");
Element e = d.getRootElement();
List<Element> ee = e.getChildren("student");
for (int i = 0; i < ee.size(); i++) {
Element eee = ee.get(i);
String id = eee.getAttributeValue("id");
String name = eee.getChildText("name");
String age = eee.getChildText("sex");
System.out.println(id + "--" + name + "---" + age);
}

4)dom4解析

  1:创建xml

Document dh = DocumentHelper.createDocument();
Element el = dh.addElement("student");
el.addAttribute("id", "100");
el.addAttribute("aa", "AA");
Element el1 = el.addElement("name");
el1.setText("zhangsan");
Element el2 = el.addElement("sex");
el2.setText("fale");
Element el3 = el.addElement("age");
el3.setText("20");

OutputFormat of = OutputFormat.createPrettyPrint();
of.setEncoding("UTF-8");
XMLWriter xw;
try {
xw = new XMLWriter(new FileOutputStream("src/ss.xml"), of);
xw.write(dh);
xw.close();
} catch (Exception e) {
e.printStackTrace();
}

2:解析xml

SAXReader sr = new SAXReader();
Document d = sr.read("src/students.xml");
Element e = d.getRootElement();
Iterator iter = e.elementIterator();
while (iter.hasNext()) {
Element ee = (Element) iter.next();
System.out.println(ee.attributeValue("age"));
System.out.println(ee.elementText("name"));
}

 

解析xml:dom,sax,jdom,dom4j,推荐dom4j

标签:

原文地址:http://www.cnblogs.com/laowu1239/p/4681856.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!