XML的解析DOM

前端之家收集整理的这篇文章主要介绍了XML的解析DOM前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1. Dom概述

Dom方式创建XML,应用了标准xml构造器javax.xml.parsers.DocumentBuilder 来创建 XML 文档,需要导入以下内容

javax.xml.parsers

javax.xml.parsers.DocumentBuilder

javax.xml.parsers.DocumentBuilderFactory

javax.xml.parsers.ParserConfigurationException;

javax.xml.transform

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

  1. /**Dom方式,创建XML*/
  2. publicStringdomCreateXML(){
  3. StringxmlWriter=null;
  4. Person[]persons=newPerson[3];//创建节点Person对象
  5. persons[0]=newPerson(1,"sunboy_2050","http://blog.csdn.net/sunboy_2050");
  6. 1]=newPerson(2,"baidu","http://www.baidu.com");
  7. @H_502_195@2]=newPerson(3,"google","http://www.google.com");
  8. try{
  9. DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
  10. DocumentBuilderbuilder=factory.newDocumentBuilder();
  11. Documentdoc=builder.newDocument();
  12. ElementeleRoot=doc.createElement("root");
  13. eleRoot.setAttribute("author","homer");
  14. eleRoot.setAttribute("date","2012-04-26");
  15. doc.appendChild(eleRoot);
  16. intpersonsLen=persons.length;
  17. for(inti=0;i<personsLen;i++){
  18. ElementelePerson=doc.createElement("person");
  19. eleRoot.appendChild(elePerson);
  20. ElementeleId=doc.createElement("id");
  21. NodenodeId=doc.createTextNode(persons[i].getId()+"");
  22. eleId.appendChild(nodeId);
  23. elePerson.appendChild(eleId);
  24. ElementeleName=doc.createElement("name");
  25. NodenodeName=doc.createTextNode(persons[i].getName());
  26. eleName.appendChild(nodeName);
  27. elePerson.appendChild(eleName);
  28. ElementeleBlog=doc.createElement("blog");
  29. NodenodeBlog=doc.createTextNode(persons[i].getBlog());
  30. eleBlog.appendChild(nodeBlog);
  31. elePerson.appendChild(eleBlog);
  32. }
  33. Propertiesproperties=newProperties();
  34. properties.setProperty(OutputKeys.INDENT,"yes");
  35. properties.setProperty(OutputKeys.MEDIA_TYPE,"xml");
  36. properties.setProperty(OutputKeys.VERSION,"1.0");
  37. properties.setProperty(OutputKeys.ENCODING,"utf-8");
  38. properties.setProperty(OutputKeys.METHOD,"xml");
  39. properties.setProperty(OutputKeys.OMIT_XML_DECLARATION,"yes");
  40. TransformerFactorytransformerFactory=TransformerFactory.newInstance();
  41. Transformertransformer=transformerFactory.newTransformer();
  42. transformer.setOutputProperties(properties);
  43. DOMSourcedomSource=newDOMSource(doc.getDocumentElement());
  44. OutputStreamoutput=newByteArrayOutputStream();
  45. StreamResultresult=newStreamResult(output);
  46. transformer.transform(domSource,result);
  47. xmlWriter=output.toString();
  48. }catch(ParserConfigurationExceptione){//factory.newDocumentBuilder
  49. e.printStackTrace();
  50. }catch(DOMExceptione){//doc.createElement
  51. }catch(TransformerFactoryConfigurationErrore){//TransformerFactory.newInstance
  52. }catch(TransformerConfigurationExceptione){//transformerFactory.newTransformer
  53. }catch(TransformerExceptione){//transformer.transform
  54. }catch(Exceptione){
  55. savedXML(fileName,xmlWriter.toString());
  56. returnxmlWriter.toString();
  57. }

运行结果:





3、Dom 解析 XML

Dom方式,解析XML是创建XML的逆过程,主要用到了builder.parse(is)进行解析,然后通过Tag、NodeList、Element、childNotes等得到Element和Node属性或值。

Code

?
    /**Dom方式,解析XML*/
  1. publicStringdomResolveXML(){
  2. StringWriterxmlWriter=newStringWriter();
  3. InputStreamis=readXML(fileName);
  4. Documentdoc=builder.parse(is);
  5. doc.getDocumentElement().normalize();
  6. NodeListnlRoot=doc.getElementsByTagName("root");
  7. ElementeleRoot=(Element)nlRoot.item(0);
  8. StringattrAuthor=eleRoot.getAttribute("author");
  9. StringattrDate=eleRoot.getAttribute("date");
  10. xmlWriter.append("root").append("\t\t");
  11. xmlWriter.append(attrAuthor).append("\t");
  12. xmlWriter.append(attrDate).append("\n");
  13. NodeListnlPerson=eleRoot.getElementsByTagName("person");
  14. intpersonsLen=nlPerson.getLength();
  15. Person[]persons=newPerson[personsLen];
  16. ElementelePerson=(Element)nlPerson.item(i);//person节点
  17. Personperson=newPerson();//创建Person对象
  18. NodeListnlId=elePerson.getElementsByTagName("id");
  19. ElementeleId=(Element)nlId.item(0);
  20. Stringid=eleId.getChildNodes().item(0).getNodeValue();
  21. person.setId(Integer.parseInt(id));
  22. NodeListnlName=elePerson.getElementsByTagName("name");
  23. ElementeleName=(Element)nlName.item( Stringname=eleName.getChildNodes().item(0).getNodeValue();
  24. person.setName(name);
  25. NodeListnlBlog=elePerson.getElementsByTagName("blog");
  26. ElementeleBlog=(Element)nlBlog.item( Stringblog=eleBlog.getChildNodes().item( person.setBlog(blog);
  27. xmlWriter.append(person.toString()).append("\n");
  28. persons[i]=person;
  29. }
  30. //factory.newDocumentBuilder
  31. e.printStackTrace();
  32. }catch(SAXExceptione){//builder.parse
  33. }catch(IOExceptione){ }catch(Exceptione){
  34. returnxmlWriter.toString();
  35. }

运行结果:





4、Person类

Person类,是创建xml的单位实例,基于Java面向对象定义的一个类

?
    publicclassPerson{
  1. privateintid;
  2. privateStringname;
  3. privateStringblog;
  4. publicPerson(){
  5. this.id=-1;
  6. this.name="";
  7. this.blog="";
  8. publicPerson(intid,Stringname,Stringblog){
  9. this.id=id;
  10. this.name=name;
  11. this.blog=blog;
  12. publicPerson(Personperson){
  13. this.id=person.id;
  14. this.name=person.name;
  15. this.blog=person.blog;
  16. publicPersongetPerson(){
  17. returnthis;
  18. publicvoidsetId(intid){
  19. publicintgetId(){
  20. returnthis.id;
  21. publicvoidsetName(Stringname){
  22. this.name=name;
  23. publicStringgetName(){
  24. returnthis.name;
  25. publicvoidsetBlog(Stringblog){
  26. publicStringgetBlog(){
  27. returnthis.blog;
  28. publicStringtoString(){
  29. return"Person\nid="+id+"\nname="+name+"\nblog="+blog+"\n";
  30. }
原文链接:https://www.f2er.com/xml/297063.html

猜你在找的XML相关文章