欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android创建与解析XML(二)——详解Dom方式

程序员文章站 2024-02-29 14:46:58
1. dom概述 dom方式创建xml,应用了标准xml构造器 javax.xml.parsers.documentbuilder 来创建 xml 文档,需要导入以下内容...

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;

创建和解析xml的效果图:
Android创建与解析XML(二)——详解Dom方式

2、dom 创建 xml

dom,借助 javax.xml.parsers.documentbuilder,可以创建 org.w3c.dom.document 对象。

使用来自 documentbuilderfactory 的 documentbuilder 对象在 android 设备上创建与解析 xml 文档。您将使用 xml pull 解析器的扩展来解析 xml 文档。

/** dom方式,创建 xml */ 
public string domcreatexml() { 
  string xmlwriter = null; 
   
  person []persons = new person[3];    // 创建节点person对象 
  persons[0] = new person(1, "sunboy_2050", "//www.jb51.net/"); 
  persons[1] = new person(2, "baidu", "http://wwwbaiducom"); 
  persons[2] = new person(3, "google", "http://wwwgooglecom"); 
   
  try { 
    documentbuilderfactory factory = documentbuilderfactorynewinstance(); 
    documentbuilder builder = factorynewdocumentbuilder(); 
    document doc = buildernewdocument(); 
     
    element eleroot = doccreateelement("root"); 
    elerootsetattribute("author", "homer"); 
    elerootsetattribute("date", "2012-04-26"); 
    docappendchild(eleroot); 
     
    int personslen = personslength; 
    for(int i=0; i<personslen; i++) { 
      element eleperson = doccreateelement("person"); 
      elerootappendchild(eleperson); 
       
      element eleid = doccreateelement("id"); 
      node nodeid = doccreatetextnode(persons[i]getid() + ""); 
      eleidappendchild(nodeid); 
      elepersonappendchild(eleid); 
 
      element elename = doccreateelement("name"); 
      node nodename = doccreatetextnode(persons[i]getname()); 
      elenameappendchild(nodename); 
      elepersonappendchild(elename); 
 
      element eleblog = doccreateelement("blog"); 
      node nodeblog = doccreatetextnode(persons[i]getblog()); 
      eleblogappendchild(nodeblog); 
      elepersonappendchild(eleblog); 
    } 
     
     
    properties properties = new properties(); 
    propertiessetproperty(outputkeysindent, "yes"); 
    propertiessetproperty(outputkeysmedia_type, "xml"); 
    propertiessetproperty(outputkeysversion, "0"); 
    propertiessetproperty(outputkeysencoding, "utf-8"); 
    propertiessetproperty(outputkeysmethod, "xml"); 
    propertiessetproperty(outputkeysomit_xml_declaration, "yes"); 
     
    transformerfactory transformerfactory = transformerfactorynewinstance(); 
    transformer transformer = transformerfactorynewtransformer(); 
    transformersetoutputproperties(properties); 
     
    domsource domsource = new domsource(docgetdocumentelement()); 
    outputstream output = new bytearrayoutputstream(); 
    streamresult result = new streamresult(output); 
    transformertransform(domsource, result); 
     
    xmlwriter = outputtostring(); 
     
  } catch (parserconfigurationexception e) {   // factorynewdocumentbuilder 
    eprintstacktrace(); 
  } catch (domexception e) {           // doccreateelement 
    eprintstacktrace(); 
  } catch (transformerfactoryconfigurationerror e) {   // transformerfactorynewinstance 
    eprintstacktrace(); 
  } catch (transformerconfigurationexception e) {   // transformerfactorynewtransformer 
    eprintstacktrace(); 
  } catch (transformerexception e) {       // transformertransform 
    eprintstacktrace(); 
  } catch (exception e) { 
    eprintstacktrace(); 
  } 
   
  savedxml(filename, xmlwritertostring()); 
   
  return xmlwritertostring(); 
} 

运行结果:
Android创建与解析XML(二)——详解Dom方式

3、dom 解析 xml

dom方式,解析xml是创建xml的逆过程,主要用到了builder.parse(is)进行解析,然后通过tag、nodelist、element、childnotes等得到element和node属性或值。

/** dom方式,解析 xml */ 
public string domresolvexml() { 
  stringwriter xmlwriter = new stringwriter(); 
   
  inputstream is= readxml(filename); 
  try { 
    documentbuilderfactory factory = documentbuilderfactorynewinstance(); 
    documentbuilder builder = factorynewdocumentbuilder(); 
    document doc = builderparse(is); 
 
    docgetdocumentelement()normalize(); 
    nodelist nlroot = docgetelementsbytagname("root"); 
    element eleroot = (element)nlrootitem(0); 
    string attrauthor = elerootgetattribute("author"); 
    string attrdate = elerootgetattribute("date"); 
    xmlwriterappend("root")append("\t\t"); 
    xmlwriterappend(attrauthor)append("\t"); 
    xmlwriterappend(attrdate)append("\n"); 
     
    nodelist nlperson = elerootgetelementsbytagname("person"); 
    int personslen = nlpersongetlength(); 
    person []persons = new person[personslen]; 
    for(int i=0; i<personslen; i++) { 
      element eleperson = (element) nlpersonitem(i);   // person节点 
      person person = new person();            // 创建person对象 
       
      nodelist nlid = elepersongetelementsbytagname("id"); 
      element eleid = (element)nliditem(0); 
      string id = eleidgetchildnodes()item(0)getnodevalue(); 
      personsetid(integerparseint(id)); 
       
      nodelist nlname = elepersongetelementsbytagname("name"); 
      element elename = (element)nlnameitem(0); 
      string name = elenamegetchildnodes()item(0)getnodevalue(); 
      personsetname(name); 
       
      nodelist nlblog = elepersongetelementsbytagname("blog"); 
      element eleblog = (element)nlblogitem(0); 
      string blog = elebloggetchildnodes()item(0)getnodevalue(); 
      personsetblog(blog); 
       
      xmlwriterappend(persontostring())append("\n"); 
      persons[i] = person; 
    } 
     
  } catch (parserconfigurationexception e) {   // factorynewdocumentbuilder 
    eprintstacktrace(); 
  } catch (saxexception e) {   // builderparse 
    eprintstacktrace(); 
  } catch (ioexception e) {    // builderparse 
    eprintstacktrace(); 
  } catch (exception e) { 
    eprintstacktrace(); 
  } 
   
  return xmlwritertostring(); 
} 

运行结果:
Android创建与解析XML(二)——详解Dom方式

4、person类

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

public class person { 
  private int id; 
  private string name; 
  private string blog; 
   
  public person() { 
    thisid = -1; 
    thisname = ""; 
    thisblog = ""; 
  } 
 
  public person(int id, string name, string blog) { 
    thisid = id; 
    thisname = name; 
    thisblog = blog; 
  } 
   
  public person(person person) { 
    thisid = personid; 
    thisname = personname; 
    thisblog = personblog; 
  } 
 
  public person getperson(){ 
    return this; 
  } 
   
  public void setid(int id) { 
    thisid = id; 
  } 
   
  public int getid(){ 
    return thisid; 
  } 
   
  public void setname(string name) { 
    thisname = name; 
  } 
 
  public string getname() { 
    return thisname; 
  } 
 
  public void setblog(string blog) { 
    thisblog = blog; 
  } 
 
  public string getblog() { 
    return thisblog; 
  } 
 
  public string tostring() { 
    return "person \nid = " + id + "\nname = " + name + "\nblog = " + blog + "\n"; 
  } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。