In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to use pull parser to write XML". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Java Code:
Case XmlPullParser.END_TAG: name = parser.getName (); if (name.equalsIgnoreCase (ITEM) & & currentMessage! = null) {messages.add (currentMessage);} else if (name.equalsIgnoreCase (CHANNEL)) {done = true;} break;} eventType = parser.next ();} catch (Exception e) {throw new RuntimeException (e);} return messages;}}
The pull parser works similar to the SAX parser. It provides similar events (start and end elements), but you need to extract them using de > (parser.next () de >). The event is sent as a numeric code, so you can use a simple case-switch. Note that parsing does not listen to the end of the element as SAX parsing does, but does most of the processing at the beginning. In the code in listing 10, when an element starts, you can call de > parser.nextText () de > to extract all character data from the XML document. Also notice that you set a tag (the Boolean variable de > donede >) to determine when the end of the content of interest is reached. This allows you to stop reading the XML document early because you know that the code will not care about the rest of the document. This can be very useful, especially if you only need to access a small number of XML documents. By stopping parsing as soon as possible, you can greatly reduce parsing time. This optimization is particularly important for mobile devices with slow connections. Pull parsers can provide some performance benefits and ease of use. It can also be used to write XML.
So far, I've focused on parsing XML through Internet. However, sometimes your application may need to send XML to a remote server. Obviously, you can use only one de > StringBuilderde > to create an XML string. Another alternative comes from the Pull parser in listing 11.
Using the pull parser to write XML
Private String writeXml (List
< Message >Messages) {XmlSerializer serializer = Xml.newSerializer (); StringWriter writer = new StringWriter (); try {serializer.setOutput (writer); serializer.startDocument ("UTF-8", true); serializer.startTag ("," messages "); serializer.attribute (", "number", String.valueOf (messages.size ()); for (Message msg: messages) {serializer.startTag ("," message "); serializer.attribute (", "date", msg.getDate ()); serializer.startTag ("," title ") Serializer.text (msg.getTitle ()); serializer.endTag ("," title "); serializer.startTag (", "url"); serializer.text (msg.getLink (). ToExternalForm ()); serializer.endTag ("," url "); serializer.startTag (", "body"); serializer.text (msg.getDescription ()); serializer.endTag ("," body "); serializer.endTag ("," message ");} serializer.endTag (", "messages") Serializer.endDocument (); return writer.toString ();} catch (Exception e) {throw new RuntimeException (e);}}
The de > XmlSerializerde > class is part of the de > XmlPullParserde > package used in the previous section. Instead of extracting events, it pushes them into the data flow or writing programs. In this case, it simply pushes the event to a de > java.io.StringWriterde > instance. It provides an intuitive API for starting and ending documents, working with elements, and adding text or attributes in a variety of ways. This is an excellent alternative to de > StringBuilderde > because it makes it easier to ensure that your XML is well structured.
That's all for "how to use the pull parser to write XML". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.