使用JDOM解析xml文件

来源:互联网 发布:响应式影视网站建设cms 编辑:程序博客网 时间:2024/05/17 04:37
public class JDomReader {public static void main(String[] args) {SAXBuilder builder = new SAXBuilder();try {Document doc = builder.build(new File("test.xml"));Element root = doc.getRootElement();System.out.println(root.getName());Element student = root.getChild("student");String id = student.getAttribute("id").getValue();System.out.println("id = " + id);student.removeChild("username");//List list = doc.getContent();//System.out.println(list.size());XMLOutputter out = new XMLOutputter(Format.getPrettyFormat().setIndent("    "));out.output(doc, new FileWriter("result.xml"));} catch (Exception e) {e.printStackTrace();}}}
<?xml version="1.0" encoding="UTF-8"?><root>    <student id="1">        <username>张三</username>    </student>    <student id="2">        <username>李四</username>    </student></root>


0 0
原创粉丝点击