//读取、修改XML
public class XmlDom4jParser {
public void modifyDocument(File inputXml) {
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(inputXml);
List list = document.selectNodes("//article/@level");
Iterator iter = list.iterator();
while (iter.hasNext()) {
Attribute attribute = (Attribute) iter.next();
if (attribute.getValue().equals("Intermediate")) attribute.setValue("Introductory");
}
list = document.selectNodes("//article/@date");
iter = list.iterator();
while (iter.hasNext()) {
Attribute attribute = (Attribute) iter.next();
if (attribute.getValue().equals("December-2001")) attribute.setValue("October-2002");
}
list = document.selectNodes("//article");
iter = list.iterator();
while (iter.hasNext()) {
Element element = (Element) iter.next();
Iterator iterator = element.elementIterator("title");
while (iterator.hasNext()) {
Element titleElement = (Element) iterator.next();
if (titleElement.getText().equals("Java configuration with XMLSchema")) {
titleElement.setText("Create flexible and extensible XML schema");
}
}
}
list = document.selectNodes("//article/author");
iter = list.iterator();
while (iter.hasNext()) {
Element element = (Element) iter.next();
Iterator iterator = element.elementIterator("firstname");
while (iterator.hasNext()) {
Element firstNameElement = (Element) iterator.next();
if (firstNameElement.getText().equals("Marcello")) firstNameElement.setText("Ayesha");
}
}
list = document.selectNodes("//article/author");
iter = list.iterator();
while (iter.hasNext()) {
Element element = (Element) iter.next();
Iterator iterator = element.elementIterator("lastname");
while (iterator.hasNext()) {
Element lastNameElement = (Element) iterator.next();
if (lastNameElement.getText().equals("Vitaletti")) lastNameElement.setText("Malik");
}
}
XMLWriter output = new XMLWriter(new FileWriter(new File("category2.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) {
XmlDom4jParser dom4jParser = new XmlDom4jParser();
dom4jParser.modifyDocument(new File("category1.xml"));
}
}
//生成XML
public class XmlDom4jUtil {
// 生成xml文档
public void generateDocument() {
Document document = DocumentHelper.createDocument();
// root node
Element catalogElement = document.addElement("catalog");
catalogElement.addComment("我的国家");
catalogElement.addProcessingInstruction("target", "text");
Element journalElement = catalogElement.addElement("journal");
journalElement.addAttribute("title", "XML Zone");
journalElement.addAttribute("publisher", "IBM developerWorks");
Element articleElement = journalElement.addElement("article");
articleElement.addAttribute("level", "Intermediate");
articleElement.addAttribute("date", "December-2001");
Element titleElement = articleElement.addElement("title");
titleElement.setText("Java configuration with XML Schema");
Element authorElement = articleElement.addElement("author");
Element firstNameElement = authorElement.addElement("firstname");
firstNameElement.setText("我的国家失望");
Element lastNameElement = authorElement.addElement("lastname");
lastNameElement.setText("Vitaletti");
// document.addDocType("catalog", null, "file://c:/Dtds/catalog.dtd");
try {
XMLWriter output = new XMLWriter(new FileWriter(new File("category1.xml")));
output.write(document);
output.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
public static void main(String[] argv) {
XmlDom4jUtil dom4j = new XmlDom4jUtil();
dom4j.generateDocument();
}
}