android下使用dom讀寫xml檔案

2021-06-25 22:58:05 字數 2107 閱讀 9929

上篇我們使用xmlserializer建立的xml檔案,發現了些問題,那樣的xml檔案是不標準的,只能自己建立自己讀,而不能供給譬如opencv的filestorage來讀取。而且,opencv能夠讀取的xml檔案還有著其他嚴格的格式控制,下面簡單介紹一下。如下所示:

<?xml version="1.0" encoding="utf-8"?>

<_>

90

其中: 是必須要有的,貌似是乙個標誌神馬,

<_>也是必須要有的,也是乙個神馬標誌。我通過以下c++程式來讀取乙個這樣的xml檔案:

filestorage fs("b.xml", filestorage::read);

if (fs.isopened())

fs.release();

}是不是很簡單,opencv 讀取xml檔案就是這麼簡單!

下面來看一下如何建立通用的xml檔案:

private void createxmlfile(listitems,outputstream out) 

rootele.a(groupele);  

transforme***ctory tf = transforme***ctory.newinstance();  

transformer transformer = tf.newtransformer();  

domsource source = new domsource(doc);  

transformer.setoutputproperty(outputkeys.encoding, "utf-8");  

transformer.setoutputproperty(outputkeys.indent, "no");  

printwriter pw = new printwriter(out);  

streamresult result = new streamresult(pw);  

transformer.transform(source, result);  

system.out.println("生成xml檔案成功!");  

} catch (parserconfigurationexcep

tion e) catch (transformerexception e)  

}  這個就是建立上述xml檔案的程式。使用的是dom來建立的。

其中的coordinate類如下:

public class coordinate  

public coordinate (string name ,int x,int y)  

public integer getid()  

public void setid(integer id)  

public string getname()  

public void setname(string name)  

public int getx()  

public void setx(int x)  

public int gety()  

public void sety(int y)  

@override  

public string tostring()  

}

我們只需要通過這樣一條語句就可以建立出需要的xml檔案了。//list1中存放建立的內容

createxmlfile(list1,new fileoutputstream(new file("/sdcard/data","c.xml")));

public listxmlreader(inputstream instream) throws exception 

else if ("x".equals(childnode.getnodename())) 

else if ("y".equals(childnode.getnodename())) 

}

}

persons.add(person);

}

instream.close();  

}catch(exception e)

return persons;

};

DOM 讀寫XML檔案

以下是個人理解 定義智慧型介面指標 include include com artptr typedef ixmldomdocument,uuidof ixmldomdocument com artptr typedef ixmldomelement,uuidof ixmldomelement co...

使用Dom4j讀寫XML簡介

解開後有兩個包,僅操作xml文件的話把dom4j 1.6.1.jar加入工程就可以了,如果需要使用xpath的話還需要加入包jaxen 1.1 beta 7.jar.以下是相關操作 一 document物件相關 1.讀取xml檔案,獲得document物件。saxreader reader new ...

Android讀寫XML(下) 建立XML文件

類說明 xmlserializerdefine an inte ce to serialziation of xml infoset.定義乙個介面來實現xml資訊的序列化。物件的串型化,也有叫做物件的序列話,並不只是簡單的把物件儲存在儲存器上,它可以使我們在流中傳輸物件,使物件變的可以像基本資料一樣...