如何利用Android XmlSerializer生成XML文件

如何利用Android XmlSerializer生成XML文件,第1张

解析xml文件的的文章很多,但是Android里生成xml文件的文章就很少了。偶然机会找到一篇相关发面的文章,就分享一下了:

用到的主要是XmlSerializer,利用它来写xml文件。

private static void XmlFileCreator(List<JokeBean>data){

File newxmlfile = new File(Environment.getExternalStorageDirectory()+"/new.xml")

try{

if(!newxmlfile.exists())

newxmlfile.createNewFile()

}catch(IOException e){

Log.e("IOException", "exception in createNewFile() method")

}

//we have to bind the new file with a FileOutputStream

FileOutputStream fileos = null

try{

fileos = new FileOutputStream(newxmlfile)

}catch(FileNotFoundException e){

Log.e("FileNotFoundException", "can't create FileOutputStream")

}

//we create a XmlSerializer in order to write xml data

XmlSerializer serializer = Xml.newSerializer()

try {

//we set the FileOutputStream as output for the serializer, using UTF-8 encoding

serializer.setOutput(fileos, "UTF-8")

//Write <?xml declaration with encoding (if encoding not null) and standalone flag (if standalone not null)

serializer.startDocument(null, Boolean.valueOf(true))

//set indentation option

serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true)

//start a tag called "root"

serializer.startTag(null, "jokes")

for(JokeBean joke:data){

serializer.startTag(null, "joke")

//i indent code just to have a view similar to xml-tree

serializer.startTag(null, "id")

serializer.text(joke.getId())

serializer.endTag(null, "id")

serializer.startTag(null, "title")

serializer.text(joke.getTitle())

//set an attribute called "attribute" with a "value" for <child2>

//serializer.attribute(null, "attribute", "value")

serializer.endTag(null, "title")

serializer.startTag(null, "text")

//write some text inside <text>

serializer.text(joke.getText())

serializer.endTag(null, "text")

serializer.endTag(null, "joke")

}

serializer.endTag(null, "jokes")

serializer.endDocument()

//write xml data into the FileOutputStream

serializer.flush()

//finally we close the file stream

fileos.close()

} catch (Exception e) {

Log.e("Exception","error occurred while creating xml file")

}

}

1. JavaScript代码如下:

复制代码代码如下:

// Changes XML to JSON

function xmlToJson(xml) {

// Create the return object

var obj = {}

if (xml.nodeType == 1) { // element

// do attributes

if (xml.attributes.length >0) {

obj["@attributes"] = {}

for (var j = 0j <xml.attributes.lengthj++) {

var attribute = xml.attributes.item(j)

obj["@attributes"][attribute.nodeName] = attribute.nodeValue

}

}

} else if (xml.nodeType == 3) { // text

obj = xml.nodeValue

}

// do children

if (xml.hasChildNodes()) {

for(var i = 0i <xml.childNodes.lengthi++) {

var item = xml.childNodes.item(i)

var nodeName = item.nodeName

if (typeof(obj[nodeName]) == "undefined") {

obj[nodeName] = xmlToJson(item)

} else {

if (typeof(obj[nodeName].length) == "undefined") {

var old = obj[nodeName]

obj[nodeName] = []

obj[nodeName].push(old)

}

obj[nodeName].push(xmlToJson(item))

}

}

}

return obj

}

2. XML代码:

复制代码代码如下:

<ALEXA VER="0.9" URL="davidwalsh.name/" HOME="0" AID="=">

<SD title="A" FLAGS="" HOST="davidwalsh.name">

<TITLE TEXT="David Walsh Blog :: PHP, MySQL, CSS, Javascript, MooTools, and Everything Else"/>

<LINKSIN NUM="1102"/>

<SPEED TEXT="1421" PCT="51"/>

</SD>

<SD>

<POPULARITY URL="davidwalsh.name/" TEXT="7131"/>

<REACH RANK="5952"/>

<RANK DELTA="-1648"/>

</SD>

</ALEXA>

3. JSON结果:

复制代码代码如下:

{

"@attributes": {

AID: "=",

HOME: 0,

URL: "davidwalsh.name/",

VER: "0.9",

},

SD = [

{

"@attributes": {

FLAGS: "",

HOST: "davidwalsh.name",

TITLE: A

},

LINKSIN: {

"@attributes": {

NUM: 1102

}

},

SPEED: {

"@attributes": {

PCT: 51,

TEXT: 1421

}

},

TITLE: {

"@attributes": {

TEXT: "David Walsh Blog :: PHP, MySQL, CSS, Javascript, MooTools, and Everything Else",

}

},

},

{

POPULARITY: {

"@attributes": {

TEXT: 7131,

URL: "davidwalsh.name/"

}

},

RANK: {

"@attributes": {

DELTA: "-1648"

}

},

REACH: {

"@attributes": {

RANK = 5952

}

}

}

]

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12075894.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存