java 操作 XML资料
java 操作 XML文件配置文件为:?xml version1.0 encodingUTF-8?config?? ?point nametest1?
java 操作 XML文件
配置文件为:
<?xml version="1.0" encoding="UTF-8"?>
<config>
?? ?<point name="test1">
?? ??? ?<name>zhang3</name>
?? ??? ?<address>abcdefg</address>
?? ??? ?<email>a@b.c</email>
?? ?</point>
</config>
?
程序中使用如下:
public static void main(String[] args) {
??? try {
?? ??? ?SAXReader reader = new SAXReader();
?? ??? ?Document? document = reader.read(new File("src/config.xml"));
?? ??? ?Element root = document.getRootElement();
?? ??? ?for(Iterator it=root.elementIterator();it.hasNext();){
??????????? Element element = (Element) it.next();
??????????? System.out.println(element.elementText("name") + element.elementText("address") + element.elementText("email"));
??????? }
?? ?} catch (DocumentException e) {
?? ??? ?e.printStackTrace();
?? ?}
}