dom 4 j操作xmlのdemo(共有)
余計なことを言わないで、直接コードしてください。
package com.cn.shop.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
/**
*
* @author NH
*
*/
public class XmlUtils {
public static Document getDocument() {
// 1. xml document
SAXReader reader = new SAXReader();
Document document = null;
try {
document = reader.read("D:\\itext\\27663.xml");
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 2. xml
/*
* String xmlFilePath = "D:\\itext\\27663.xml"; try { document =
* DocumentHelper.parseText(xmlFilePath); } catch (DocumentException e)
* { // TODO Auto-generated catch block e.printStackTrace(); } // 3.
* Document document = DocumentHelper.createDocument(); Element root =
* document.addElement("csdn");
*/
return document;
}
public static void anaXml() throws Exception {
// xml document
SAXReader reader = new SAXReader();
try {
Document document = reader.read("D:\\itext\\27663.xml");
Element root = document.getRootElement();
System.out.println(root.getName());
getElement(root);
/* elementMethod(root); */
/*
* // Element element = root.element("title");
*
*
* // element id Attribute attr = element.attribute("id");
* // element.remove(attr);
*
* // element.addAttribute("author", " ");
*
* // Element newElement = root.addElement("where"); //
* newElement.setText(" , ");
*
* // element where Element author =
* element.element("where"); // boolean flag =
* element.remove(author); // true ,
* System.out.println(flag); // CDATA
* element.addCDATA(" , ."); // writer(document);
*/
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
*
*
*/
public static void getElement(Element root) {
//
List<Attribute> as = root.attributes();
for (Attribute a : as) {
System.out.println(" :" + a.getName());
/*
* System.out.println(" :" + a.getText());
*
* System.out.println(" :" + a.getValue());
*/
}
if (!root.getTextTrim().equals("")) {
System.out.println(" ::::" + root.getText());
}
Iterator<Element> el = root.elementIterator();
while (el.hasNext()) {
//
Element e = el.next();
//
getElement(e);
}
}
/**
* Element element elements
*
* @param node
*/
public static void elementMethod(Element node) {
// node , 。
Element e = node.element("info");
// , chapter ( )
Element author = e.element("classification");
System.out.println(e.getName() + "----" + author.getText());
// , classification 。
List<Element> authors = e.elements("classification");
for (Element aut : authors) {
System.out.println(aut.getText());
}
// 。
List<Element> elements = e.elements();
for (Element el : elements) {
System.out.println(el.getText());
}
}
/**
* document
*
* @param document
* @throws Exception
*/
public static void writer(Document document) throws Exception {
//
// OutputFormat format = OutputFormat.createCompactFormat();
//
OutputFormat format = OutputFormat.createPrettyPrint();
//
format.setEncoding("UTF-8");
// XMLWriter ,
/*
* XMLWriter writer = new XMLWriter(new OutputStreamWriter(new
* FileOutputStream(new File("src//a.xml")), "UTF-8"), format);
*/
File file = new File("c://index// .xml");
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
XMLWriter writer = new XMLWriter(osw);
//
writer.write(document);
//
writer.flush();
//
writer.close();
}
// xml
public void test() throws Exception {
// saxreader
SAXReader reader = new SAXReader();
// , Document
Document document = reader.read(new File("src//c.xml"));
//
Element root = document.getRootElement();
//
String docXmlText = document.asXML();
System.out.println(docXmlText);
System.out.println("---------------------------");
// csdn
String rootXmlText = root.asXML();
System.out.println(rootXmlText);
System.out.println("---------------------------");
// java
Element e = root.element("java");
System.out.println(e.asXML());
}
/**
* document document xml
*
* @throws Exception
*/
public void test2() throws Exception {
Document document = DocumentHelper.createDocument();//
Element root = document.addElement("csdn");
Element java = root.addElement("java");
java.setText("java ");
Element ios = root.addElement("ios");
ios.setText("ios ");
writer(document);
}
/**
* Document
*
* @throws Exception
*/
public void test1() throws Exception {
String text = "<csdn><java>Java </java><net>Net </net></csdn>";
Document document = DocumentHelper.parseText(text);
Element e = document.getRootElement();
System.out.println(e.getName());
writer(document);
}
/**
* document
*
* @param document
* @throws Exception
*/
public void writer1(Document document) throws Exception {
//
// OutputFormat format = OutputFormat.createCompactFormat();
//
OutputFormat format = OutputFormat.createPrettyPrint();
//
format.setEncoding("UTF-8");
// XMLWriter ,
// XMLWriter writer = new XMLWriter(new FileWriter(new
// File("src//a.xml")),format);
XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(new File("src//c.xml")), "UTF-8"),
format);
//
writer.write(document);
//
writer.flush();
//
writer.close();
}
public static void main(String[] args) {
try {
anaXml();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
以上のdom 4 j操作xmlのデモは小编で皆さんにお伝えした内容の全てです。参考にしていただければと思います。どうぞよろしくお愿いします。