Solution of using pull parser to operate XML file in Android

1、 Use the pull parser to read the XML file

In addition to parsing XML files using Sax or DOM, you can also parse XML files using Android's built-in pull parser. The pull parser is an open source java project that can be used for both Android and Java EE. If it is used in JavaEE, you need to put its jar file into the classpath. Because Android has been integrated into the pull parser, there is no need to add any jar file. The various XML files used by Android system itself are also parsed by pull parser. The pull parser works similarly to the Sax parser. It provides similar events, such as start element and end element events. You can use parser. Next () to enter the next element and trigger the corresponding event. Unlike Sax, the event generated by the pull parser is a number rather than a method, so you can use a switch to process the event of interest. When the element starts parsing, call the parser. Nexttext () method to get the value of the next text type node

2、 Use the pull parser to generate XML files

Sometimes, we need to generate an XML file. There are many methods to generate XML files. For example, we can use only one StringBuilder to assemble XML content, and then write the content to the file; You can use DOM API to generate XML files, or you can use pull parser to generate XML files. It is recommended to use pull parser here.

Use the pull parser to generate a myljq.xml file with the same content as the ljq.xml file. The code is noted at the bottom of this page

The usage code is as follows (generate XML file):

File xmlFile = new File("myljq.xml");

FileOutputStream outStream = new FileOutputStream(xmlFile);

OutputStreamWriter outStreamWriter = new OutputStreamWriter(outStream,"UTF-8");

BufferedWriter writer = new BufferedWriter(outStreamWriter);

writeXML(persons,writer);

writer.flush();

writer.close();

If you only want to get the generated XML string content, you can use stringwriter:

StringWriter writer = new StringWriter();

writeXML(persons,writer);

String content = writer.toString();

Case:

123.xml

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>