※xml.xml
<?xml version="1.0" encoding="UTF-8"?> <root>top1 top2 <first fa="f1" fb="1f"> first-1 <second1 sa="s1">aaa</second1> <second2>bbb</second2> <second3>ccc</second3> </first> top3 <first fa="f2" fb="2f"> first-2 <second1 sa="s2">ddd</second1> <second2>eee</second2> <second3>fff</second3> </first> top4 <first fa="f3" fb="3f"> first-3 <second1 sa="s3">ggg</second1> <second2>hhh</second2> <second3>iii</second3> </first> </root>
※
※maven
<dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency>
※讀檔
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; public class DOM4jTest { public static void main(String[] args) throws DocumentException { SAXReader reader = new SAXReader(); Document doc = reader.read("src/main/java/xml.xml"); Element root = doc.getRootElement(); System.out.println("方法一"); List<Element> first = root.elements(); for (Element e : first) { System.out.println(e.getText().trim()); for (Object o : e.attributes()) { System.out.println(((Attribute) o).getValue()); } System.out.println("子元素"); List<Element> second = e.elements(); for (Element s : second) { System.out.println(s.getText().trim()); } System.out.println(System.getProperty("line.separator")); } System.out.println("方法二"); Iterator<Element> it2 = root.elementIterator(); while (it2.hasNext()) { Element firstTwo = (Element) it2.next(); // ... } System.out.println("方法三"); for (Iterator<Element> it3 = root.elementIterator(); it3.hasNext();) { Element firstThree = it3.next(); // ... } } }
※方法二和三,迴圈裡的內容和方法一一樣
※使用XPath
※要使用 XPath 必需要有 jaxen 的 jar,否則執行selectSingleNode方法時會報錯
<dependency> <groupId>jaxen</groupId> <artifactId>jaxen</artifactId> <version>1.1.6</version> </dependency>
※
import java.io.IOException; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.SAXReader; import org.dom4j.tree.DefaultAttribute; public class DOM4jTest { public static void main(String[] args) throws IOException, DocumentException { SAXReader reader = new SAXReader(); Document doc = reader.read("src/main/java/xml.xml"); Node root = doc.selectSingleNode("/root"); List<Element> second1 = root.selectNodes("first/second1"); for (Element e : second1) { System.out.println(e.getName()); System.out.println(e.getText()); System.out.println(e.attributeValue("sa")); System.out.println("---------------"); } System.out.println("==============="); List<Element> first = root.selectNodes("first"); for (Element e : first) { System.out.println(e.getName()); System.out.println(e.getText().trim()); for (Object o : e.attributes()) { DefaultAttribute ele = (DefaultAttribute) o; System.out.println(ele.getStringValue()); } System.out.println("==============="); } } }
※selectSingleNode 和 selectNodes 可以寫XPath
※寫檔
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; public class DOM4jTest { public static void main(String[] args) throws IOException { Document doc = DocumentHelper.createDocument(); Element root = doc.addElement("root"); Element first = root.addElement("first"); Element second1 = first.addElement("second1"); Element second2 = first.addElement("second2"); Element second3 = first.addElement("second3"); root.setText("top1" + System.getProperty("line.separator") + "\ttop2"); first.setText("first-1"); second1.setText("aaa"); second2.setText("bbb"); second3.setText("ccc"); first.addAttribute("fa", "f1"); first.addAttribute("fb", "1f"); second1.addAttribute("sa", "s1"); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); File file = new File("D:/" + File.separator + "xml.xml"); OutputStream fout = new FileOutputStream(file, true); XMLWriter out = new XMLWriter(fout, format); out.write(doc); out.close(); System.out.println("XML已生成"); } }
※此例只是寫其中一個first而已
※變數format是讓產生出的XML有排版
沒有留言:
張貼留言