<?
xml
version="1.0" encoding="UTF-8"?>
<?
xml-stylesheet
type="text/xsl" href="students.xsl"?>
<
students
>
<!--A Student Catalog-->
<
student
sn="01">
<
name
>
sam
</
name
>
<
age
>
18
</
age
>
</
student
>
<
student
sn="02">
<
name
>
lin
</
name
>
<
age
>
20
</
age
>
</
student
>
</
students
>
|
import
java.io.File;
import
java.io.FileWriter;
import
java.io.IOException;
import
java.util.HashMap;
import
java.util.Map;
import
org.dom4j.Document;
import
org.dom4j.DocumentException;
import
org.dom4j.DocumentHelper;
import
org.dom4j.Element;
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(
"sam"
);
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(
"lin"
);
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"
));
}
}
|
import
java.io.File;
import
java.io.FileWriter;
import
java.io.IOException;
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.SAXReader;
import
org.dom4j.io.XMLWriter;
public
class
XmlMod {
public
void
modifyDocument(File inputXml) {
try
{
SAXReader saxReader =
new
SAXReader();
Document document = saxReader.read(inputXml);
List list = document.selectNodes(
"//students/student/@sn"
);
Iterator iter = list.iterator();
while
(iter.hasNext()) {
Attribute attribute = (Attribute) iter.next();
if
(attribute.getValue().equals(
"01"
))
attribute.setValue(
"001"
);
}
list = document.selectNodes(
"//students/student"
);
iter = list.iterator();
while
(iter.hasNext()) {
Element element = (Element) iter.next();
Iterator iterator = element.elementIterator(
"name"
);
while
(iterator.hasNext()) {
Element nameElement = (Element) iterator.next();
if
(nameElement.getText().equals(
"sam"
))
nameElement.setText(
"jeff"
);
}
}
XMLWriter output =
new
XMLWriter(
new
FileWriter(
new
File(
"students-modified.xml"
)));
output.write(document);
output.close();
}
catch
(DocumentException e) {
System.
out
.println(e.getMessage());
}
catch
(IOException e) {
System.
out
.println(e.getMessage());
}
}
public
static
void
main(String[] argv) {
XmlMod dom4jParser =
new
XmlMod();
dom4jParser.modifyDocument(
new
File(
"students-gen.xml"
));
}
}
|
import
java.io.File;
import
java.util.Iterator;
import
org.dom4j.Attribute;
import
org.dom4j.Document;
import
org.dom4j.DocumentException;
import
org.dom4j.Element;
import
org.dom4j.ProcessingInstruction;
import
org.dom4j.VisitorSupport;
import
org.dom4j.io.SAXReader;
public
class
XmlTra {
private
File
inputXml
;
public
XmlTra(File inputXml) {
this
.
inputXml
= inputXml;
}
public
Document getDocument() {
SAXReader saxReader =
new
SAXReader();
Document document =
null
;
try
{
document = saxReader.read(
inputXml
);
}
catch
(DocumentException e) {
e.printStackTrace();
}
return
document;
}
public
Element getRootElement() {
return
getDocument().getRootElement();
}
public
void
traversalDocumentByIterator() {
Element root = getRootElement();
//
枚举根节点下所有子节点
for
(Iterator ie = root.elementIterator(); ie.hasNext();) {
System.
out
.println(
"======"
);
Element element = (Element) ie.next();
System.
out
.println(element.getName());
//
枚举属性
for
(Iterator ia = element.attributeIterator(); ia.hasNext();) {
Attribute attribute = (Attribute) ia.next();
System.
out
.println(attribute.getName() +
":"
+ attribute.getData());
}
//
枚举当前节点下所有子节点
for
(Iterator ieson = element.elementIterator(); ieson.hasNext();) {
Element elementSon = (Element) ieson.next();
System.
out
.println(elementSon.getName() +
":"
+ elementSon.getText());
}
}
}
public
void
traversalDocumentByVisitor() {
getDocument().accept(
new
MyVisitor());
}
/**
*
定义自己的访问者类
*/
private
static
class
MyVisitor
extends
VisitorSupport {
/**
*
对于属性节点,打印属性的名字和值
*/
public
void
visit(Attribute node) {
System.
out
.println(
"attribute : "
+ node.getName() +
" = "
+ node.getValue());
}
/**
*
对于处理指令节点,打印处理指令目标和数据
*/
public
void
visit(ProcessingInstruction node) {
System.
out
.println(
"PI : "
+ node.getTarget() +
" "
+ node.getText());
}
/**
*
对于元素节点,判断是否只包含文本内容,如是,则打印标记的名字和
元素的内容。如果不是,则只打印标记的名字
*/
public
void
visit(Element node) {
if
(node.isTextOnly())
System.
out
.println(
"element : "
+ node.getName() +
" = "
+ node.getText());
else
System.
out
.println(
"--------"
+ node.getName() +
"--------"
);
}
}
public
static
void
main(String[] argv) {
XmlTra dom4jParser =
new
XmlTra(
new
File(
"students-gen.xml"
));
// dom4jParser.traversalDocumentByIterator();
dom4jParser.traversalDocumentByVisitor();
}
}
|
public
class
MyVisitor
extends
VisitorSupport {
public
void
visit(Element element) {
System.
out
.println(element.getName());
}
public
void
visit(Attribute attr) {
System.
out
.println(attr.getName());
}
}
|
import
java.io.File;
import
org.dom4j.DocumentException;
import
org.dom4j.Element;
import
org.dom4j.ElementHandler;
import
org.dom4j.ElementPath;
import
org.dom4j.io.SAXReader;
public
class
XmlHandler {
public
static
void
main(String[] args) {
SAXReader saxReader =
new
SAXReader();
File file =
new
File(
"students.xml"
);
try
{
//
添加一个
ElementHandler
实例。
saxReader.addHandler(
"/students/student"
,
new
StudentHandler());
saxReader.read(file);
}
catch
(DocumentException e) {
System.
out
.println(e.getMessage());
}
}
/**
*
定义
StudentHandler
处理器类,对
<student>
元素进行处理。
*/
private
static
class
StudentHandler
implements
ElementHandler {
public
void
.Start(ElementPath path) {
Element elt = path.getCurrent();
System.
out
.println(
"Found student: "
+ elt.attribut.ue(
"sn"
));
//
添加对子元素
<name>
的处理器。
path.addHandler(
"name"
,
new
NameHandler());
}
public
void
.End(ElementPath path) {
//
移除对子元素
<name>
的处理器。
path.removeHandler(
"name"
);
}
}
/**
*
定义
NameHandler
处理器类,对
<student>
的
<name>
子元素进行处理。
*/
private
static
class
NameHandler
implements
ElementHandler {
public
void
.Start(ElementPath path) {
System.
out
.println(
"path : "
+ path.getPath());
}
public
void
.End(ElementPath path) {
Element elt = path.getCurrent();
//
输出
<name>
元素的名字和它的文本内容。
System.
out
.println(elt.getName() +
" : "
+ elt.getText());
}
}
}
|
import
javax.xml.transform.Transformer;
import
javax.xml.transform.TransformerFactory;
import
org.dom4j.Document;
import
org.dom4j.io.DocumentResult;
import
org.dom4j.io.DocumentSource;
……
public
Document styleDocument(Document document, String stylesheet)
throws
Exception {
// load the transformer using JAXP
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(
new
StreamSource(stylesheet));
// now lets style the given document
DocumentSource source =
new
DocumentSource(document);
DocumentResult result =
new
DocumentResult();
transformer.transform(source, result);
// return the transformed document
Document transformedDoc = result.getDocument();
return
transformedDoc;
}
……
|
本文出自 “子 孑” 博客,请务必保留此出处http://zhangjunhd.blog.51cto.com/113473/126310