1. Dom概述
Dom方式创建XML,应用了标准xml构造器javax.xml.parsers.DocumentBuilder 来创建 XML 文档,需要导入以下内容
javax.xml.parsers
javax.xml.transformjavax.xml.parsers.DocumentBuilder
javax.xml.parsers.DocumentBuilderFactory
javax.xml.parsers.ParserConfigurationException;
javax.xml.transform.TransformerFactory
javax.xml.transform.Transformer
javax.xml.transform.dom.DOMSource
javax.xml.transform.stream.StreamResult
javax.xml.transform.OutputKeys;
javax.xml.transform.TransformerFactoryConfigurationError;
javax.xml.transform.TransformerConfigurationException;
javax.xml.transform.TransformerException;
org.w3c.dom
org.w3c.dom.Document;
org.w3c.dom.Element;
org.w3c.dom.Node;
org.w3c.dom.DOMException;
org.w3c.dom.NodeList;
org.xml.sax.SAXException;
sdk源码查看路径(google code)
创建和解析xml的效果图:
2、Dom 创建 XML
Dom,借助 javax.xml.parsers.DocumentBuilder,可以创建 org.w3c.dom.Document 对象。
使用来自 DocumentBuilderFactory 的 DocumentBuilder 对象在 Android 设备上创建与解析 XML 文档。您将使用 XML pull 解析器的扩展来解析 XML 文档。
Code
- /**Dom方式,创建XML*/ @H_301_113@ publicStringdomCreateXML(){
- StringxmlWriter=null;
- Person[]persons=newPerson[3];//创建节点Person对象
- persons[0]=newPerson(1,"sunboy_2050","http://blog.csdn.net/sunboy_2050");
- 1]=newPerson(2,"baidu","http://www.baidu.com");
- 2]=newPerson(3,"google","http://www.google.com");
- try{
- DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
- DocumentBuilderbuilder=factory.newDocumentBuilder();
- Documentdoc=builder.newDocument();
- ElementeleRoot=doc.createElement("root");
- eleRoot.setAttribute("author","homer");
- eleRoot.setAttribute("date","2012-04-26");
- doc.appendChild(eleRoot);
- intpersonsLen=persons.length;
- for(inti=0;i<personsLen;i++){
- ElementelePerson=doc.createElement("person");
- eleRoot.appendChild(elePerson);
- ElementeleId=doc.createElement("id");
- NodenodeId=doc.createTextNode(persons[i].getId()+"");
- eleId.appendChild(nodeId);
- elePerson.appendChild(eleId);
- ElementeleName=doc.createElement("name");
- NodenodeName=doc.createTextNode(persons[i].getName());
- eleName.appendChild(nodeName);
- elePerson.appendChild(eleName);
- ElementeleBlog=doc.createElement("blog");
- NodenodeBlog=doc.createTextNode(persons[i].getBlog());
- eleBlog.appendChild(nodeBlog);
- elePerson.appendChild(eleBlog);
- }
- Propertiesproperties=newProperties();
- properties.setProperty(OutputKeys.INDENT,"yes");
- properties.setProperty(OutputKeys.MEDIA_TYPE,"xml");
- properties.setProperty(OutputKeys.VERSION,"1.0");
- properties.setProperty(OutputKeys.ENCODING,"utf-8");
- properties.setProperty(OutputKeys.METHOD,"xml");
- properties.setProperty(OutputKeys.OMIT_XML_DECLARATION,"yes");
- TransformerFactorytransformerFactory=TransformerFactory.newInstance();
- Transformertransformer=transformerFactory.newTransformer();
- transformer.setOutputProperties(properties);
- DOMSourcedomSource=newDOMSource(doc.getDocumentElement());
- OutputStreamoutput=newByteArrayOutputStream();
- StreamResultresult=newStreamResult(output);
- transformer.transform(domSource,result);
- xmlWriter=output.toString();
- }catch(ParserConfigurationExceptione){//factory.newDocumentBuilder
- e.printStackTrace();
- }catch(DOMExceptione){//doc.createElement
- }catch(TransformerFactoryConfigurationErrore){//TransformerFactory.newInstance
- }catch(TransformerConfigurationExceptione){//transformerFactory.newTransformer
- }catch(TransformerExceptione){//transformer.transform
- }catch(Exceptione){
- savedXML(fileName,xmlWriter.toString());
- returnxmlWriter.toString();
- }
运行结果:
3、Dom 解析 XML
Dom方式,解析XML是创建XML的逆过程,主要用到了builder.parse(is)进行解析,然后通过Tag、NodeList、Element、childNotes等得到Element和Node属性或值。
Code