参考:
http://zhangjunhd.blog.51cto.com/113473/126310/
所需要的包:
dom4j-2.0.0-ALPHA-2.jar
jaxen-1.1.4.jar 用于语句.selectNodes("//students/student/@sn");
XML如下:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<students>
<!--An Student Catalog-->
<student sn="01">
<name>Yerasel</name>
<age>18</age>
</student>
<student sn="02">
<name>Rudo</name>
<age>20</age>
</student>
</students>
代码如下:
package yerasel;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class XmlGen {
public Document generateDocumentByMethod() {
Document document = DocumentHelper.createDocument();
// ProcessingInstruction
Map<String, String> inMap = new HashMap<String, String>();
inMap.put("type", "text/xsl");
inMap.put("href", "students.xsl");
document.addProcessingInstruction("xml-stylesheet", inMap);
// root element
Element studentsElement = document.addElement("students");
studentsElement.addComment("An Student Catalog");
// son element
Element stuElement = studentsElement.addElement("student");
stuElement.addAttribute("sn", "01");
Element nameElement = stuElement.addElement("name");
nameElement.setText("Yerasel");
Element ageElement = stuElement.addElement("age");
ageElement.setText("18");
// son element
Element anotherStuElement = studentsElement.addElement("student");
anotherStuElement.addAttribute("sn", "02");
Element anotherNameElement = anotherStuElement.addElement("name");
anotherNameElement.setText("Rudo");
Element anotherAgeElement = anotherStuElement.addElement("age");
anotherAgeElement.setText("20");
return document;
}
public Document generateDocumentByString() {
String text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<?xml-stylesheet type=\"text/xsl\" href=\"students.xsl\"?>"
+ "<students><!--An Student Catalog--> <student sn=\"01\">"
+ "<name>sam</name><age>18</age></student><student sn=\"02\">"
+ "<name>lin</name><age>20</age></student></students>";
Document document = null;
try {
document = DocumentHelper.parseText(text);
} catch (DocumentException e) {
e.printStackTrace();
}
return document;
}
public void saveDocument(Document document, File outputXml) {
try {
// 美化格式
OutputFormat format = OutputFormat.createPrettyPrint();
/*
* // 缩减格式 OutputFormat format = OutputFormat.createCompactFormat();
*/
/*
* // 指定XML编码 format.setEncoding("GBK");
*/
XMLWriter output = new XMLWriter(new FileWriter(outputXml), format);
output.write(document);
output.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
public static void main(String[] argv) {
XmlGen dom4j = new XmlGen();
Document document = null;
document = dom4j.generateDocumentByMethod();
// document = dom4j.generateDocumentByString();
dom4j.saveDocument(document, new File("output.xml"));
SAXReader saxReader = new SAXReader();
try {
document = saxReader.read("output.xml");
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<Attribute> list = (List<Attribute>) document
.selectNodes("//students/student/@sn");
Iterator<Attribute> iter = list.iterator();
Element root = document.getRootElement();
Element foo;
Iterator<Element> i;
i = root.elementIterator("student");
while (iter.hasNext()) {
Attribute attribute = (Attribute) iter.next(); // 标签属性
foo = (Element) i.next();// 标签子节点元素
if (attribute.getValue().equals("02")) {
String sin = attribute.getValue();
System.out.println("sin = " + sin);
System.out.println("得到指定属性下的元素文本");
System.out.println("name = " + foo.elementText("name"));
System.out.println("age = " + foo.elementText("age"));
// 列出所有属性,可以将IP、Port写入Conn标签的属性<Conn1 IP=2334, Port=133>
System.out.println("遍历属性");
for (Attribute e : list) {
System.out.println(e.getName() + " " + e.getText());
}
}
}
// 全部遍历
System.out.println("全部遍历");
root = document.getRootElement();
i = root.elementIterator("student");
while (i.hasNext()) {
foo = (Element) i.next();
System.out.println("name = " + foo.elementText("name"));
System.out.println("age = " + foo.elementText("age"));
}
}
}
sin = 02
得到指定属性下的元素文本
name = Rudo
age = 20
遍历属性
sn 01
sn 02
全部遍历
name = Yerasel
age = 18
name = Rudo
age = 20