android dom方式创建xml

http://blog.csdn.net/nxh_love/article/details/7085174

android dom 解析xml方式文章中,简单介绍了dom解析xml的应用。今天在原文章的基础上,说一下android中dom创建xml的应用。

首先:创建的文件会放在/data/data/cn.com.xxx(当前包名)/files下面。

创建生成的xml文件如下所示:

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <classes>  
  3.     <group name="一年级" num="10">  
  4.         <person name="小明" age="7">  
  5.             <chinese>语文90</chinese>  
  6.             <english>英语80</english>  
  7.         </person>  
  8.     </group>  
  9. </classes>  

可以直接用android dom 解析xml方式文章中方法去解析,注意修改一点:

[html] view plaincopy
 
  1. // 从assets文件夹下获取文件 转换成输入流  
  2. //          inStream = this.getResources().getAssets().open(fileName);  
  3. //          doc = docBuilder.parse(inStream);  
  4.             InputStream fosStream = openFileInput(fileName);  
  5.             doc = docBuilder.parse(fosStream);  

同时,fileName取得方法:

[html] view plaincopy
 
  1. String[] fileNames = getFilesDir().list();  
[html] view plaincopy
 
  1. String fileName = fileNames[0];  

解析出来的结果是


以下是创建xml文件的代码:

[html] view plaincopy
 
    1. private void createXmlFile(){  
    2.           
    3.         try {  
    4.             DocumentBuilderFactory factory = DocumentBuilderFactory  
    5.                     .newInstance();  
    6.             DocumentBuilder builder = factory.newDocumentBuilder();  
    7.             Document doc  = builder.newDocument();  
    8.             //创建xml根元素  
    9.             Element rootEle = doc.createElement("classes");  
    10.             doc.appendChild(rootEle);  
    11.             //创建xml二级元素  
    12.             Element groupEle = doc.createElement("group");  
    13.             groupEle.setAttribute("name", "一年级");  
    14.             groupEle.setAttribute("num", "10");  
    15.             //创建xml person元素  
    16.             Element personEle = doc.createElement("person");  
    17.             //personEle 的属性和属性值  
    18.             personEle.setAttribute("name", "小明");  
    19.             personEle.setAttribute("age", "7");  
    20.             //创建personELe的子元素  
    21.             Element chinese = doc.createElement("chinese");  
    22.             //创建personELe的子元素的值  
    23.             chinese.appendChild(doc.createTextNode("语文90"));  
    24.             personEle.appendChild(chinese);  
    25.             Element english = doc.createElement("english");  
    26.             english.appendChild(doc.createTextNode("英语80"));  
    27.             personEle.appendChild(english);  
    28.               
    29.             groupEle.appendChild(personEle);  
    30.             rootEle.appendChild(groupEle);  
    31.               
    32.             TransformerFactory tf = TransformerFactory.newInstance();  
    33.             Transformer transformer = tf.newTransformer();  
    34.               
    35.             DOMSource source = new DOMSource(doc);  
    36.             transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");  
    37.             transformer.setOutputProperty(OutputKeys.INDENT, "no");  
    38.             //创建文件存放在 /data/data/cn.xxx.xxx(当前包)/files   
    39.             FileOutputStream fos = openFileOutput("Dom.xml", Context.MODE_PRIVATE);  
    40.             //创建文件存放在 /data/data/cn.xxx.xxx(当前包)/cache  
    41. //          FileOutputStream fos = Op  
    42.             PrintWriter pw = new PrintWriter(fos);  
    43.             StreamResult result = new StreamResult(pw);  
    44.             transformer.transform(source, result);  
    45.               
    46.             System.out.println("生成XML文件成功!");  
    47.         } catch (ParserConfigurationException e) {  
    48.             System.out.println(e.getMessage());  
    49.         } catch (TransformerConfigurationException e) {  
    50.             System.out.println(e.getMessage());  
    51.         } catch (TransformerException e) {  
    52.             System.out.println(e.getMessage());  
    53.         } catch (FileNotFoundException e) {  
    54.             System.out.println(e.getMessage());  
    55.         }  
    56.           
    57.     }  
原文地址:https://www.cnblogs.com/daishuguang/p/3857321.html