xml tutorials

This commit is contained in:
sxyx2008
2014-07-09 11:07:27 +08:00
parent cafa386420
commit 78e6dd27fc
48 changed files with 2741 additions and 2 deletions
+20
View File
@@ -0,0 +1,20 @@
<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-jdom-example</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,56 @@
package net.aimeizi.jdom.modify.example;
import java.io.FileWriter;
import java.io.IOException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class ModifyXMLFile {
/**
* @param args
*/
public static void main(String[] args) {
try {
SAXBuilder builder = new SAXBuilder();
Document doc = (Document) builder.build(ClassLoader
.getSystemResource("file.xml"));
Element rootNode = doc.getRootElement();
// update staff id attribute
Element staff = rootNode.getChild("staff");
staff.getAttribute("id").setValue("2");
// add new age element
Element age = new Element("age").setText("28");
staff.addContent(age);
// update salary value
staff.getChild("salary").setText("7000");
// remove firstname element
staff.removeChild("firstname");
XMLOutputter xmlOutput = new XMLOutputter();
// display nice nice
xmlOutput.setFormat(Format.getPrettyFormat());
xmlOutput.output(doc, new FileWriter("target/file.xml"));
// xmlOutput.output(doc, System.out);
System.out.println("File updated!");
} catch (IOException io) {
io.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
}
}
}
@@ -0,0 +1,46 @@
package net.aimeizi.jdom.parse.example;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class ParseXMLFile {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
SAXBuilder builder = new SAXBuilder();
try {
Document document = (Document) builder.build(ClassLoader.getSystemResource("file.xml"));
Element rootNode = document.getRootElement();
List<Element> list = rootNode.getChildren("staff");
for (int i = 0; i < list.size(); i++) {
Element node = (Element) list.get(i);
System.out.println("First Name : "
+ node.getChildText("firstname"));
System.out.println("Last Name : "
+ node.getChildText("lastname"));
System.out.println("Nick Name : "
+ node.getChildText("nickname"));
System.out.println("Salary : " + node.getChildText("salary"));
System.out.println();
}
} catch (IOException io) {
System.out.println(io.getMessage());
} catch (JDOMException jdomex) {
System.out.println(jdomex.getMessage());
}
}
}
@@ -0,0 +1,53 @@
package net.aimeizi.jdom.write.example;
import java.io.FileWriter;
import java.io.IOException;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class WriteXMLFile {
public static void main(String[] args) {
try {
Element company = new Element("company");
Document doc = new Document(company);
doc.setRootElement(company);
Element staff = new Element("staff");
staff.setAttribute(new Attribute("id", "1"));
staff.addContent(new Element("firstname").setText("yong"));
staff.addContent(new Element("lastname").setText("mook kim"));
staff.addContent(new Element("nickname").setText("mkyong"));
staff.addContent(new Element("salary").setText("199999"));
doc.getRootElement().addContent(staff);
Element staff2 = new Element("staff");
staff2.setAttribute(new Attribute("id", "2"));
staff2.addContent(new Element("firstname").setText("low"));
staff2.addContent(new Element("lastname").setText("yin fong"));
staff2.addContent(new Element("nickname").setText("fong fong"));
staff2.addContent(new Element("salary").setText("188888"));
doc.getRootElement().addContent(staff2);
// new XMLOutputter().output(doc, System.out);
XMLOutputter xmlOutput = new XMLOutputter();
// display nice nice
xmlOutput.setFormat(Format.getPrettyFormat());
xmlOutput.output(doc, new FileWriter("target/file.xml"));
System.out.println("File Saved!");
} catch (IOException io) {
System.out.println(io.getMessage());
}
}
}
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<company>
<staff id="1">
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
<staff id="2">
<firstname>low</firstname>
<lastname>yin fong</lastname>
<nickname>fong fong</nickname>
<salary>200000</salary>
</staff>
</company>