Java解析xml文件
读xml文件:
xml文件内容:
<?xml version="1.0" encoding="UTF-8"?> <Languages cat="it"> <lan id="1"> <name>Java</name> <ide>Eclipse</ide> </lan> <lan id="2"> <name>Swift</name> <ide>Xcode</ide> </lan> <lan id="3"> <name>C#</name> <ide>Visual Studio</ide> </lan> </Languages>
代码:
package XMLParse; import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class ReadXML { public static void main(String[] args) { try { //DOM方式解析xml文件 DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document document=builder.parse(new File("languages.xml")); Element root=document.getDocumentElement(); System.out.println("categary:"+root.getAttribute("cat")); NodeList list=root.getElementsByTagName("lan"); for (int i = 0; i < list.getLength(); i++) { Element lan= (Element) list.item(i); System.out.println("*******************"); System.out.println("id="+lan.getAttribute("id")); NodeList cList=lan.getChildNodes(); //会将文件里的换行也当成子节点返回到cList中 for (int j = 0; j < cList.getLength(); j++) { Node c= cList.item(j); if(c instanceof Element) { //因为cList中包含了空格和换行,所以需要判断是否是元素 System.out.println(c.getNodeName()+"="+c.getTextContent()); } } } } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
注意:NodeList cList=lan.getChildNodes(); 会把原文件中的换行也当作子节点,所以需要判断:if(c instanceof Element){……}
创建xml文件:
import java.io.File; import java.io.StringWriter; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; public class creaetXML { public static void main(String[] args) { try { DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document document=builder.newDocument(); Element root=document.createElement("Languages"); root.setAttribute("cat", "IT"); Element lan1=document.createElement("lan"); lan1.setAttribute("id", "1"); Element name1=document.createElement("name"); name1.setTextContent("java"); Element ide1=document.createElement("ide"); ide1.setTextContent("eclipse"); lan1.appendChild(name1); lan1.appendChild(ide1); root.appendChild(lan1); document.appendChild(root); Element lan2=document.createElement("lan"); lan2.setAttribute("id", "2"); Element name2=document.createElement("name"); name2.setTextContent("c#"); Element ide2=document.createElement("ide"); ide2.setTextContent("visual studio"); lan2.appendChild(name2); lan2.appendChild(ide2); root.appendChild(lan2); //*************以下为输出************** TransformerFactory transformerFactory=TransformerFactory.newInstance(); Transformer transformer=transformerFactory.newTransformer(); StringWriter writer=new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(writer));//输出到控制台 System.out.println(writer.toString()); transformer.transform(new DOMSource(document), new StreamResult(new File("newxml.xml"))); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } } }
其中重点是如何输出:
TransformerFactory transformerFactory=TransformerFactory.newInstance(); Transformer transformer=transformerFactory.newTransformer(); StringWriter writer=new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(writer));//输出到控制台 System.out.println(writer.toString()); transformer.transform(new DOMSource(document), new StreamResult(new File("newxml.xml")));
使用第三方库dom4j操作xml数据:
package testDom4j; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; public class testDom4j { public static void main(String[] args) { String xmlString="<root><people>panghu</people></root>"; try { Document document=DocumentHelper.parseText(xmlString); System.out.println(document.asXML()); } catch (DocumentException e) { e.printStackTrace(); } } }
效率高很多。