Posted on 2009-06-17 21:52
eric_xu 阅读(184)
评论(0) 编辑 收藏 所属分类:
Java
package sjtu.rfid;
import java.io.File;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ParserXML {
public void Parser(File inputXml){
try{
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(inputXml);
Element root = document.getRootElement();
System.out.println(root.element("username").getText());
System.out.println(root.element("password").getText());
}catch(DocumentException e){
e.printStackTrace();
}
}
public static void main(String[] args){
ParserXML parser = new ParserXML();
parser.Parser(new File("config.xml"));
}
}
package sjtu.rfid;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
public class CreateXML {
public void createXML(){
Document document = DocumentHelper.createDocument();
Element configElement = document.addElement("config");
configElement.addComment("This is my configueration.");
Element usernameElement = configElement.addElement("username");
Element passwordElement = configElement.addElement("password");
usernameElement.addAttribute("id", "97");
usernameElement.setText("Eric");
passwordElement.setText("1234");
try {
XMLWriter output = new XMLWriter(new FileWriter(new File(
"d:/config.xml")));
output.write(document);
output.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
public static void main(String[] args) {
CreateXML create = new CreateXML();
create.createXML();
}
}
Nested exception
XML格式错误