There are three ways to organize data in an HTTP network transfer:
1. Html method
2. XML mode
3. JSON mode
the structure of the XML is parsed as follows:
1. Node
2. Elements
3. Attributes and attribute values
Because XML is highly extensible, it requires a stable base rule to support extensions, which is:
1. Start and end tag matching
2. Nested tags cannot be nested with each other
3. Case-sensitive
in Android, there are three ways to parse XML data:
1. DOM (Org.w3c.dom)
The "Document Object Model" method, the parsed XML will generate a tree-like object.
2. SAX (Org.xml.sax)
Simple API for XML, notifies the program in the form of an event, parses the XML.
3, Xmlpull (ORG.XMLPULL.V1)
Similar to the SAX approach, the program parses the XML in a "pull" manner.
Introduction to SAX Technology
Sax is an event-driven XML API that defines an event stream that can specify the XML structure of the code that is passed from the parser to a specialized handler, simply speaking, it parses fast and consumes less memory of the parser. This parser is better suited for mobile devices such as Android.
The advantages of using sax are:
Because the advantage of Sax is the way the stream is handled, when a tag is encountered, it does not record the currently encountered label.
In other words, the information that you know in the Starteelment method is just the name and attribute of the current signature, as for the nested structure of the tag, the name of the upper tag, and whether the child element is related to other structures is not known.
Introduction to DOM technology
DOM is an object model for XML documents that can be used to directly access various parts of an XML document, where the document in the DOM is modeled as a tree, where each component of the XML syntax represents a node, and the DOM allows the user to traverse the document tree, moving from the parent node to the child node and the sibling node. and take advantage of attributes specific to a node type (elements have attributes, text nodes have text data)
Node (each component in an XML document is a node)
This is what the DOM provides:
The entire document is a node document
Each XML tag is an element node
The text contained in the XML element is a text node
Each XML attribute is an attribute node
Introduction to pull technology
In addition to using both of these parsing XML files, we can also parse the XML file using the pull that comes with Java.
The pull parser runs in a similar fashion to the SAX parser, which provides similar events.
such as starting and ending elements, using parser.next () to make the next element and triggering the corresponding event, the event is sent as code, so you can use a switch to select the event and then handle it accordingly. When the element is parsed, the Parser.nexttext () method is called to get the next element of the text type.
Pull Features:
Simple structure: An interface, an exception, a factory-composed pull parser
Easy to use: The pull parser has only one important method next method, which is used to retrieve the next event, and it has only 5 common properties:
START DOCUMENT
Start_tag
TEXT
End_tag
End_document
from memory usage:
Sax and pull have less memory resolution than DOM, and are better suited for Android phone development.
Here's a code example to illustrate how sax is used:
Myhandle.java
Package Com.sax.handler;import Java.util.arraylist;import Java.util.hashmap;import java.util.list;import Java.util.jar.attributes.name;import Org.xml.sax.attributes;import Org.xml.sax.saxexception;import Org.xml.sax.helpers.defaulthandler;public class MyHandler extends DefaultHandler {private hashmap<string, String > map = null;//stores a single parsed full object private listSaxservice.java
Package com.sax.service;
Import Java.io.InputStream;
Import Java.util.HashMap;
Import java.util.List;
Import Javax.xml.parsers.SAXParser;
Import Javax.xml.parsers.SAXParserFactory;
Import Com.sax.handler.MyHandler;
public class Saxservice {
Public Saxservice () {
TODO auto-generated Constructor stub
}
public static listInputStream InputStream, String nodeName) {
try {
Create a factory object that parses the XML
SAXParserFactory SPF = saxparserfactory.newinstance ();
SAXParser parser = Spf.newsaxparser ();//Parse XML
MyHandler handler = new MyHandler (nodeName);
Parser.parse (InputStream, handler);
Inputstream.close ();
return Handler.getlist ();
} catch (Exception e) {
Todo:handle exception
}
return null;
}
}
Httputil.java
Package com.sax.http;
Import Java.io.InputStream;
Import java.net.HttpURLConnection;
Import Java.net.URL;
public class Httputils {
Public Httputils () {
TODO auto-generated Constructor stub
}
public static InputStream GetXML (String path) {
InputStream inputstream = null;
try {
URL url = new URL (path);
if (URL! = null) {
HttpURLConnection connection = (httpurlconnection) URL
. OpenConnection ();
Connection.setconnecttimeout (3000);
Connection.setdoinput (TRUE);
Connection.setrequestmethod ("GET");
int code = Connection.getresponsecode ();
if (code = = 200) {
InputStream = Connection.getinputstream ();
}
}
} catch (Exception e) {
Todo:handle exception
}
return inputstream;
}
}
Test.java->main
Package com.sax.test;
Import Java.io.InputStream;
Import Java.util.HashMap;
Import java.util.List;
Import Com.sax.http.HttpUtils;
Import Com.sax.service.SaxService;
public class Test {
Public Test () {
TODO auto-generated Constructor stub
}
/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
String Path = "Http://192.168.0.102:8080/myhttp/person.xml";
InputStream InputStream = httputils.getxml (path);
try {
listInputStream, "person");
For (hashmap<string, string> map:list) {
System.out.println (Map.tostring ());
}
} catch (Exception e) {
Todo:handle exception
}
}
}
Android to XML file parsing