2013年7月27日星期六

SAX parsing xml file

In SAX parsing an XML file with only implemented startDocument () method
The remaining few are executed is not a problem where
------ Solution - -------------------------------------------
parameters Mody

 public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException { }  
------ Solution ----------------------------------- ---------
put your best parse the code and your xml
------ Solution ---------------- ----------------------------
http://blog.csdn.net/xiaanming/article/details/8937746 SAX parsing XML landlord can look
------ For reference only ---------------------------------------
the content is MainActivity
package com.example.activity18;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import xmlParse.XmlInfo;
import xmlParse.XmlParse;

import com.example.download.DownLoad;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ListActivity {

private XmlParse xmlparse = new XmlParse ();
private static final String strurl = "http://10.0.2.2/mldn/a3.xml";
@ Override
public boolean onOptionsItemSelected (MenuItem item) {
/ / TODO Auto-generated method stub
if (item.getItemId () == 1)
{
/ / System.out.println ("itemid ----->" + item.getItemId ());
downloadXML (strurl);


}
else if (item.getItemId () == 2)
{
System.out.println ("itemid ----->" + item.getItemId ());
}
return super.onOptionsItemSelected (item);
}

@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
}

@ Override
public boolean onCreateOptionsMenu (Menu menu) {
/ / Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater (). inflate (R.menu.main, menu);
menu.add (0, 1, 1, R.string.update);
menu.add (0, 2, 2, R.string.about);
return true;
}
public String downloadXML (String strxml)
{
DownLoad download = new DownLoad ();
String s = "";
try {
s = download.downloadFile (strxml);
System.out.println ("1111111111111111");
SAXParser (s);
System.out.println ("22222222222");
showList ();
System.out.println ("333333333333");
} catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
return s;

}
public String SAXParser (String str)
{
SAXParserFactory factory = SAXParserFactory.newInstance ();
try {
XMLReader reader = factory.newSAXParser (). getXMLReader ();
reader.setContentHandler (xmlparse);
try {
reader.parse (new InputSource (new StringReader (str)));
} catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}

} catch (SAXException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
} catch (ParserConfigurationException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
return null;
}
public void showList ()
{
System.out.println ("4444444444444");
System.out.println (xmlparse.list.size ());
for (XmlInfo info: xmlparse.list) {

System.out.println (info);
}
}

}



This is DownLoad content
package com.example.download;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class DownLoad {

public String downloadFile (String str) throws IOException
{
URL url = new URL (str);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection ();
BufferedReader br = new BufferedReader (new InputStreamReader (urlConn.getInputStream ()));
String str2 = null;
String str1 = null;
while ((str1 = br.readLine ())! = null)
{
/ / System.out.println (str1);
str2 + = str1;
}
/ / System.out.println ("s2 =" + str2);

br.close ();
return str2;
}

}



This is XmlParse content
package xmlParse;


import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XmlParse extends DefaultHandler {

String tagName = null;
public static List list = new ArrayList ();
XmlInfo info;

@ Override
public void characters (char [] ch, int start, int length)
throws SAXException {
/ / TODO Auto-generated method stub
super.characters (ch, start, length);
System.out.println ("characters");
String str = new String (ch, start, length);
if (tagName.equals ("mp3_name"))
{
info.setMp3_name (str);
}
else if (tagName.equals ("mp3_size"))
{
info.setMp3_size (str);
}
else if (tagName.equals ("lrc_name"))
{
info.setLrc_name (str);
}
else if (tagName.equals ("lrc_size"))
{
info.setLrc_size (str);
}
else if (tagName.equals ("id"))
{
info.setId (str);
}
}

@ Override
public void endDocument () throws SAXException {
/ / TODO Auto-generated method stub
super.endDocument ();
System.out.println ("endDocument");
}

@ Override
public void endElement (String uri, String localName, String qName)
throws SAXException {
/ / TODO Auto-generated method stub
super.endElement (uri, localName, qName);
System.out.println ("endElement");
if (qName.equals ("resource"))
{
info.toString ();
list.add (info);
}
tagName = "";
}

@ Override
public void startDocument () throws SAXException {
/ / TODO Auto-generated method stub
/ / super.startDocument ();
System.out.println ("startDocument");
}

@ Override
public void startElement (String uri, String localName, String qName,
Attributes attributes) throws SAXException {
/ / TODO Auto-generated method stub
super.startElement (uri, localName, qName, attributes);
System.out.println ("startElement");
tagName = localName;
if (tagName.equals ("resource"))
{
info = new XmlInfo ();
}

}



}

This is XmlInfo content
package xmlParse;

public class XmlInfo {
public String getId () {
return id;
}
public void setId (String id) {
this.id = id;
}
public String getMp3_name () {
return mp3_name;
}
public void setMp3_name (String mp3_name) {
this.mp3_name = mp3_name;
}
public String getMp3_size () {
return mp3_size;
}
public void setMp3_size (String mp3_size) {
this.mp3_size = mp3_size;
}
public String getLrc_name () {
return lrc_name;
}
public void setLrc_name (String lrc_name) {
this.lrc_name = lrc_name;
}
public String getLrc_size () {
return lrc_size;
}
public void setLrc_size (String lrc_size) {
this.lrc_size = lrc_size;
}
@ Override
public String toString () {
/ / TODO Auto-generated method stub
System.out.println (id);
return null;
}
private String id;
private String mp3_name;
private String mp3_size;
private String lrc_name;
private String lrc_size;


}

------ For reference only ---------------------------------- -----
probably a3.xml server to read the file and then parse each contents are placed in a new Object XmlInfo then deposited in the collection of the last list element in the collection output
This is a3.xml content:



1001
zhangjie_mp3
11092822
zhangjie_lrc
1205


------ For reference only ----------------------------- ----------
Thank you upstairs to answer finally found str read xml file into a problem with this process eventually led to read xml file format error < br>

没有评论:

发表评论