解析xml文件:
1,dom
dom是用于平台和语言无关的方式表示xml文档的W3C标准,把xml文档转化成一个包含其内容的树,并可以对树进行遍历。
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
读:
Document doc=builder.parse(File file);
NodeList nl=doc.getElementByTagName("NodeName");
for(int i=0;i<nl.getLength();i++){
doc.getElementByTagName("childNodeName").item(i).getFirstChild().getNodeValue();
doc.getElementByTagName("childNodeName").item(i).getFirstChild().getNodeValue();
}
写:
Document doc=builder.parse(File file);//如果是要创建新的xml:Document doc=builder.newDocument();
Element root=doc.getDocumentElement("rootName"); //得到跟节点
Element childNode=doc.createElement("childNodeName"); //创建一个子节点
childNode.setAttribute("name","id"); //设置属性
childNode.setTextContent("value"); //设置内容
doc.appendChild(root);
root.appendChild(childNode);
TranformerFactory factory=TranformerFactory.newInstance();
Tranformer tranformer=factory.newTranformer();
tranformer.tranformer(new Resource(doc),new StreanResult(new File("demo.xml")))
package dom; import java.io.File; /** * 使用dom解析xml文件:DecumentBuilderFactory dbf=DecumentBuilderFactory.newInstace() * DecumentBuilder db=dbf.newDocumentBuilder() * Document d=db.parse(new File(path)) * NodeList */ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; 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; import org.w3c.dom.NodeList; public class DomXml { public static void main(String[] args) { writeXml(); readXml(); } public static void readXml(){ try{ File file=new File("demo.xml"); DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); DocumentBuilder db=dbf.newDocumentBuilder(); Document doc=db.parse(file); NodeList nl=doc.getElementsByTagName("info"); for(int i=0;i<nl.getLength();i++){ System.out.println("name="+doc.getElementsByTagName("name").item(i).getFirstChild().getNodeValue()); System.out.println("password="+doc.getElementsByTagName("password").item(i).getFirstChild().getNodeValue()); } }catch(Exception e){ e.printStackTrace(); } } public static void writeXml(){ try{ File file=new File("demo.xml"); DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); DocumentBuilder db=dbf.newDocumentBuilder(); // Document doc=db.newDocument(); Document doc=db.parse(file); Element root=doc.getDocumentElement(); Element info=doc.createElement("info"); Element info1=doc.createElement("info"); info.setAttribute("name", "admin2"); info.setAttribute("id", "2"); info.setTextContent("ddd=dasd"); info1.setAttribute("name", "admin2"); info1.setAttribute("id", "2"); info1.setTextContent("ddd=dasd"); root.appendChild(info); root.appendChild(info1); TransformerFactory factory = TransformerFactory.newInstance(); Transformer former = factory.newTransformer(); former.transform(new DOMSource(doc), new StreamResult(new File("demo.xml"))); }catch(Exception e){ e.printStackTrace(); } } }
2,SAX
不需要将数据存储在内存中,不必解析整个文档,满足条件即可截至解析。采用事件模型
SAXParserFactory factory=SAXParserFactory.newInstance();
SAXParser saxparser = factory.newSAXParser();
SaxHandler handler = new SaxHandler();
saxparser.parse("dom.xml",handler);
class SaxHandler extends DefaultHandler{
startDecument(); //开始解析
endDecument(); //结束解析
startElement(url,localName,qName,attribute){
if("childNodeName".equals(qName)){
print("属性:"+attribute.getQName);
print("值:"+attribute.getValue);
}
}
endElement(url,localName,qName,attribute){}
characters(char[],int,length){
String NodeValue=new String(char,start,length);
print(NodeValue);
}
}
package dom; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; class SaxHandler extends DefaultHandler{ @Override public void startDocument() throws SAXException { // TODO Auto-generated method stub super.startDocument(); System.out.println("-------------解析开始----------------"); } @Override public void endDocument() throws SAXException { // TODO Auto-generated method stub super.endDocument(); System.out.println("------------解析结束------------------"); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { // TODO Auto-generated method stub super.startElement(uri, localName, qName, attributes); if(qName.equals("info")){ int length=attributes.getLength(); for(int i=0;i<length;i++){ System.out.print("属性:"+attributes.getQName(i)); System.out.println("值:"+attributes.getValue(i)); } }else{ System.out.println("节点:"+qName); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { // TODO Auto-generated method stub super.endElement(uri, localName, qName); } @Override public void characters(char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub super.characters(ch, start, length); String flag=new String(ch,start,length); if(!("".equals(flag.trim()))){ System.out.println("节点值为:"+flag); } System.out.println(); } } public class SaxXml { public static void main(String[] args) { try{ SAXParserFactory factory=SAXParserFactory.newInstance(); //创建解析工厂 SAXParser saxParser=factory.newSAXParser(); //创建解析器 SaxHandler handler=new SaxHandler(); saxParser.parse("demo.xml", handler); }catch(Exception e){ e.printStackTrace(); } } }
3,JDOM
目的成为java特定文档模型,简化与XML的交互并且比使用DOM更快
4,DOM4J