xml tutorials
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>net.aimeizi</groupId>
|
||||
<artifactId>xml-tutorials</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
<artifactId>xml-dom-example</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
|
||||
|
||||
</project>
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package net.aimeizi.dom.example;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
/**
|
||||
* 统计元素个数
|
||||
* @author welcome
|
||||
*
|
||||
*/
|
||||
public class CountXMLElement {
|
||||
|
||||
public static void main(String argv[]) {
|
||||
|
||||
try {
|
||||
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
|
||||
Document doc = docBuilder.parse(ClassLoader.getSystemResourceAsStream("books.xml"));
|
||||
|
||||
NodeList list = doc.getElementsByTagName("book");
|
||||
|
||||
System.out.println("Total of elements : " + list.getLength());
|
||||
|
||||
} catch (ParserConfigurationException pce) {
|
||||
pce.printStackTrace();
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
} catch (SAXException sae) {
|
||||
sae.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package net.aimeizi.dom.example;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.transform.OutputKeys;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
public class DomCreateXML {
|
||||
|
||||
public static void main(String[] args) throws Exception{
|
||||
DocumentBuilder builder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||
Document document = builder.newDocument();
|
||||
Element rootElement=document.createElement("root");
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
Element bookElement=document.createElement("book");
|
||||
bookElement.setAttribute("id", (i+1)+"");
|
||||
|
||||
Element nameElement=document.createElement("name");
|
||||
nameElement.setTextContent("Spring");
|
||||
|
||||
Element priceElement=document.createElement("price");
|
||||
priceElement.setTextContent("88");
|
||||
|
||||
Element authorElement=document.createElement("author");
|
||||
authorElement.setTextContent("李刚");
|
||||
|
||||
rootElement.appendChild(bookElement);
|
||||
bookElement.appendChild(nameElement);
|
||||
bookElement.appendChild(priceElement);
|
||||
bookElement.appendChild(authorElement);
|
||||
}
|
||||
|
||||
document.appendChild(rootElement);
|
||||
|
||||
TransformerFactory factory=TransformerFactory.newInstance();
|
||||
Transformer transformer=factory.newTransformer();
|
||||
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
|
||||
transformer.transform(new DOMSource(document), new StreamResult(new File("target/1.xml")));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package net.aimeizi.dom.example;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
/**
|
||||
* 使用dom解析xml
|
||||
* @author welcome
|
||||
*
|
||||
*/
|
||||
public class DomXmlParser {
|
||||
|
||||
public static void main(String[] args) {
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder documentBuilder = null;
|
||||
try {
|
||||
documentBuilder = factory.newDocumentBuilder();
|
||||
} catch (ParserConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Document document = null;
|
||||
try {
|
||||
document = documentBuilder.parse(ClassLoader.getSystemResourceAsStream("books.xml"));
|
||||
} catch (SAXException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Element element = document.getDocumentElement();
|
||||
NodeList nodeList = element.getElementsByTagName("book");
|
||||
for (int i = 0; i < nodeList.getLength(); i++) {
|
||||
Element book = (Element) nodeList.item(i);
|
||||
String id = book.getAttribute("id");
|
||||
|
||||
Element bookname = (Element) book.getElementsByTagName("name")
|
||||
.item(0);
|
||||
String name = bookname.getFirstChild().getNodeValue();
|
||||
|
||||
Element bookauthor = (Element) book.getElementsByTagName("author")
|
||||
.item(0);
|
||||
String author = bookauthor.getFirstChild().getNodeValue();
|
||||
|
||||
Element bookprice = (Element) book.getElementsByTagName("price")
|
||||
.item(0);
|
||||
String price = bookprice.getFirstChild().getNodeValue();
|
||||
|
||||
Element bookpubinfo = (Element) book
|
||||
.getElementsByTagName("pubinfo").item(0);
|
||||
String pubinfo = bookpubinfo.getFirstChild().getNodeValue();
|
||||
|
||||
System.out.println(id + "," + name + "," + author + "," + price
|
||||
+ "," + pubinfo);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
package net.aimeizi.dom.example;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NamedNodeMap;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
/**
|
||||
* 编辑xml
|
||||
* @author welcome
|
||||
*
|
||||
*/
|
||||
public class ModifyXMLFile {
|
||||
|
||||
public static void main(String argv[]) {
|
||||
|
||||
try {
|
||||
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
|
||||
Document doc = docBuilder.parse(ClassLoader.getSystemResourceAsStream("file.xml"));
|
||||
|
||||
// Get the root element
|
||||
//Node company = doc.getFirstChild();
|
||||
|
||||
// Get the staff element , it may not working if tag has spaces, or
|
||||
// whatever weird characters in front...it's better to use
|
||||
// getElementsByTagName() to get it directly.
|
||||
// Node staff = company.getFirstChild();
|
||||
|
||||
// Get the staff element by tag name directly
|
||||
Node staff = doc.getElementsByTagName("staff").item(0);
|
||||
|
||||
// update staff attribute
|
||||
NamedNodeMap attr = staff.getAttributes();
|
||||
Node nodeAttr = attr.getNamedItem("id");
|
||||
nodeAttr.setTextContent("2");
|
||||
|
||||
// append a new node to staff
|
||||
Element age = doc.createElement("age");
|
||||
age.appendChild(doc.createTextNode("28"));
|
||||
staff.appendChild(age);
|
||||
|
||||
// loop the staff child node
|
||||
NodeList list = staff.getChildNodes();
|
||||
|
||||
for (int i = 0; i < list.getLength(); i++) {
|
||||
|
||||
Node node = list.item(i);
|
||||
|
||||
// get the salary element, and update the value
|
||||
if ("salary".equals(node.getNodeName())) {
|
||||
node.setTextContent("2000000");
|
||||
}
|
||||
|
||||
//remove firstname
|
||||
if ("firstname".equals(node.getNodeName())) {
|
||||
staff.removeChild(node);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// write the content into xml file
|
||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||
Transformer transformer = transformerFactory.newTransformer();
|
||||
DOMSource source = new DOMSource(doc);
|
||||
StreamResult result = new StreamResult(new File("target/file.xml"));
|
||||
transformer.transform(source, result);
|
||||
|
||||
System.out.println("Done");
|
||||
|
||||
} catch (ParserConfigurationException pce) {
|
||||
pce.printStackTrace();
|
||||
} catch (TransformerException tfe) {
|
||||
tfe.printStackTrace();
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
} catch (SAXException sae) {
|
||||
sae.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<data>
|
||||
<book id="1">
|
||||
<name>Android应用开发详解</name>
|
||||
<author>json</author>
|
||||
<price>88</price>
|
||||
<pubinfo>人民邮电出版社</pubinfo>
|
||||
</book>
|
||||
<book id="2">
|
||||
<name>Android权威指南</name>
|
||||
<author>tom</author>
|
||||
<price>79</price>
|
||||
<pubinfo>人民教育出版社</pubinfo>
|
||||
</book>
|
||||
<book id="3">
|
||||
<name>Android开发案例大全</name>
|
||||
<author>mark</author>
|
||||
<price>68</price>
|
||||
<pubinfo>电子工业出版社</pubinfo>
|
||||
</book>
|
||||
<book id="4">
|
||||
<name>Android从入门到精通</name>
|
||||
<author>jack</author>
|
||||
<price>68</price>
|
||||
<pubinfo>电子工业出版社</pubinfo>
|
||||
</book>
|
||||
<book id="5">
|
||||
<name>Pro Spring</name>
|
||||
<author>mark</author>
|
||||
<price>68</price>
|
||||
<pubinfo>电子工业出版社</pubinfo>
|
||||
</book>
|
||||
</data>
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
|
||||
<company>
|
||||
<staff id="1">
|
||||
<firstname>yong</firstname>
|
||||
<lastname>mook kim</lastname>
|
||||
<nickname>mkyong</nickname>
|
||||
<salary>100000</salary>
|
||||
</staff>
|
||||
</company>
|
||||
Reference in New Issue
Block a user