Chapter 9 Summary 3 Parsing XML Format Data

First, install an Apache server
After installation, create a new file named get_data.xml in the Apache\htdocs directory:

<apps>
	<app>
		<id>1</id>
		<name>Google Maps</name>
		<version>1.0</version>
	</app>
	<app>
		<id>2</id>
		<name>Chrome</name>
		<version>2.1</version>
	</app>
	<app>
		<id>3</id>
		<name>Google Play</name>
		<version>2.3</version>
	</app>

</apps>
  1. Pull-style parsing of XML files
    1. Get an instance of XmlPullParseFactory
    2. Getting XmlPullParse objects from an instance of XmlPullParseFactory
    3. Call the setInput() method of the XmlPullParse object, set the xmlData returned by the server, and start parsing
      1. Parsing begins:
        1. Get the current parse event using the getEventType() method of the XmlPullParse object
        2. If the current parsing event is not equal to xmlPullParse.END_DOCUMENT, the parsing is not completed. Call the next() method to get the next parsing event.
        3. In the while loop, the name of the current node is obtained by the getName() method. If the name of the current node equals id,name, or version, the nextNext() method is called to get the content of the current node. When an app node is resolved, the content is printed out

Override parseXMLWithPull(String xmlData) which parses data:

private void parseXMLWithPull(String xmlData){
	try{
		XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
		XmlPullParser xmlPullParse = factory.newPullParser();
		xmlPullParser.setInput(new StringReader(xmlData));
		int eventType = xmlPullParser.getEventType();
		String id = "";
		String name = "";
		String version = "";
		while(eventType != XmlPullParser.END_DOCUMENT){
			Stirng nodeName = xmlPullParser.getName();
			switch(eventType){
				//Start parsing a node
				case XmlPullParser.START_TAG:
					{
						if("id".equals(nodeName))
							id=xmlPullParser.nextNext();
						else if("name".equals(nodeName))
							name = xmlPullParser.nextNext();
						else if("version".equals(nodeName))
							version = xmlPullParser.nextNext()
						break;
					}
				//Complete parsing a node
				case XmlPullParser.END_TAG:{
					if("app".equals(nodeName)){
						Logd
						Logd
						Logd
					}
					break;
				}
				eventType = xmlPullParser.next();
		}
		}catch(Exception e){
			e.printStackTrace();
		
		}
}
		

SAX parsing:
First, create a new ContentHandler class of your own that inherits the DefaultHandler class, and override the five methods of the parent class:

1. public void startDocument()
	Initialize each StringBuilder
2. public void endDocument()
	Call the endDocument() method of the parent class
3. public void startElement(String uri,String localName,String qNAme,Attributes attributes)
	If the current node name
4. public void endElement(String uri,String localName,String qName)
	After analyzing a node, print out the contents of the node and empty the contents of each StringBuilder
5. public void characters(char[] ch,int start,int length)
	Get the content of the node based on the current node name, and decide which StringBuilder to add the content to based on the node name

Then write a parseXMLWithSAX method in the main activity:

private void parseXMLWithSAX(Stirng xmlData){
	try{
		SAXParserFactory factory = SAXParserFActory.newInstance();
		XMLReader xmlReader = factory.newSAXParser().getXMLReader();
		ContentHandler handler = new ContentHandler();
		//Setting handler in XMLReader
		xmlReader.setContentHandler(handler);
		//Start parsing
		xmlReader.parse(new InputSource(new StringReader(xmlData)));
	}catch(Exception e){
		e.printStackTrace()
	}
}
Fifteen original articles were published, 3 were praised, and 3138 were visited
Private letter follow

Tags: Apache xml Google

Posted on Sun, 02 Feb 2020 19:27:53 -0800 by GreenMarch