输出 创建XML 保存
Document document = new Document(); Element root = new Element("root"); document.addContent(root); Comment comment = new Comment("This is my comments"); root.addContent(comment); Element e = new Element("hello"); e.setAttribute("sohu","www.sohu.com"); root.addContent(e); Element e2 = new Element("world"); Attribute attr = new Attribute("test","hehe"); e2.setAttribute(attr); e.addContent(e2); e2.addContent(new Element("aaa").setAttribute("a","b") .setAttribute("x","y").setAttribute("gg","hh").setText("text content")); Format format = Format.getPrettyFormat(); format.setIndent(" "); // format.setEncoding("gbk"); XMLOutputter out = new XMLOutputter(format); out.output(document,new FileWriter("jdom.xml"));
输入 读取XML 解析
SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(new File("jdom.xml")); Element element = doc.getRootElement(); System.out.println(element.getName()); Element hello = element.getChild("hello"); System.out.println(hello.getText()); List list = hello.getAttributes(); for(int i = 0 ;i < list.size(); i++) { Attribute attr = (Attribute)list.get(i); String attrName = attr.getName(); String attrValue = attr.getValue(); System.out.println(attrName + "=" + attrValue); } hello.removeChild("world"); XMLOutputter out = new XMLOutputter(Format.getPrettyFormat().setIndent(" ")); out.output(doc,new FileOutputStream("jdom2.xml"));
1 对于Jdom的format类的getRawFormat方法,通常用于 XML数据的网络传输,这种格式会去掉不必要的空格,减少数据量。
2dom4j(www.dom4j.org)
dom4j 创建XML文档
// 创建文档并设置文档的根元素节点 :第一种方式 // Document document = DocumentHelper.createDocument(); // // Element root = DocumentHelper.createElement("student"); // // document.setRootElement(root); // 创建文档并设置文档的根元素节点 :第二种方式 Element root = DocumentHelper.createElement("student"); Document document = DocumentHelper.createDocument(root); root.addAttribute("name","zhangsan"); Element helloElement = root.addElement("hello"); Element worldElement = root.addElement("world"); helloElement.setText("hello"); worldElement.setText("world"); helloElement.addAttribute("age","20"); XMLWriter xmlWriter = new XMLWriter(); xmlWriter.write(document); OutputFormat format = new OutputFormat(" ",true); XMLWriter xmlWriter2 = new XMLWriter(new FileOutputStream("student2.xml"),format); xmlWriter2.write(document); XMLWriter xmlWriter3 = new XMLWriter(new FileWriter("student3.xml"),format); xmlWriter3.write(document); xmlWriter3.close();
解析XML文档
SAXReader saxReader = new SAXReader(); Document doc = saxReader.read(new File("student2.xml")); Element root = doc.getRootElement(); System.out.println("root element: " + root.getName()); List childList = root.elements(); System.out.println(childList.size()); List childList2 = root.elements("hello"); System.out.println(childList2.size()); Element first = root.element("hello"); System.out.println(first.attributeValue("age")); for(Iterator iter = root.elementIterator(); iter.hasNext();) { Element e = (Element)iter.next(); System.out.println(e.attributeValue("age")); } System.out.println("---------------------------"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); org.w3c.dom.Document document = db.parse(new File("student2.xml")); DOMReader domReader = new DOMReader(); //将JAXP的Document转换为dom4j的Document Document d = domReader.read(document); Element rootElement = d.getRootElement(); System.out.println(rootElement.getName());原文链接:https://www.f2er.com/xml/298143.html