假设您需要一个ArrayList<Movie>
,其中的结构Movie
类是
public class Movie
{
private ArrayList<File> files;
public Movie()
{
this.files = new ArrayList<File>();
}
}
and the File
的结构是
public class File
{
private int type;
private String url;
private String path;
private String title;
}
与必要的getter
and setter
函数,您可以通过以下方式获取所需的列表
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
final MovieXmlHandler handler = new MovieXmlHandler();
parser.parse(new InputSource(new StringReader(yourXmlString)), handler);
final ArrayList<Movie> movies = handler.getRecords();
where
-
yourXmlString
是xml数据
您已粘贴在上面,并且
-
handler
是一个 MovieXmlHandler
实例。
MovieXmlHandler 实现:
public class MovieXmlHandler extends DefaultHandler
{
private static final String TAG_MOVIE = "movie";
private static final String TAG_FILE = "file";
private static final String TAG_TYPE = "type";
private static final String TAG_URL = "url";
private static final String TAG_PATH = "path";
private static final String TAG_TITLE = "title";
private String currentNodeName;
private Movie currentMovie;
private File currentFile;
private ArrayList<Movie> records = null;
private String elementValue;
public ArrayList<Movie> getRecords()
{
return records;
}
@Override
public void startDocument() throws SAXException
{
super.startDocument();
this.records = new ArrayList<Movie>();
}
@Override
public void startElement(final String Uri, final String localName,
final String qName, final Attributes att) throws SAXException
{
if (localName != null)
currentNodeName = localName;
}
@Override
public void characters(final char[] ch, final int start,
final int length) throws SAXException
{
if (this.currentNodeName == null)
return;
this.elementValue = new String(ch, start, length).trim();
if (this.currentNodeName.equalsIgnoreCase(TAG_MOVIE))
this.currentMovie = new Movie();
if (this.currentNodeName.equalsIgnoreCase(TAG_FILE))
this.currentFile = new File();
else if (this.currentNodeName.equalsIgnoreCase(TAG_TYPE))
this.currentFile.setType(Integer.parseInt(this.elementValue));
else if (this.currentNodeName.equalsIgnoreCase(TAG_URL))
this.currentFile.setUrl(this.elementValue);
else if (this.currentNodeName.equalsIgnoreCase(TAG_PATH))
this.currentFile.setPath(this.elementValue);
else if (this.currentNodeName.equalsIgnoreCase(TAG_TITLE))
this.currentFile.setTitle(this.elementValue);
}
@Override
public void endElement(final String Uri, final String localName,
final String qName) throws SAXException
{
if (localName.equalsIgnoreCase(TAG_MOVIE))
{
if (this.currentMovie != null)
this.records.add(this.currentMovie);
}
else if (localName.equalsIgnoreCase(TAG_FILE))
{
if ((this.currentMovie != null) && (this.currentFile != null))
this.currentMovie.getFiles().add(this.currentFile);
}
currentNodeName = null;
}
}
当然,如果你只有 xml 数据的 url (xmlUrl:String
), 您可以使用
final URL sourceUrl = new URL(xmlURL);
final SAXParser sp = SAXParserFactory.newInstance().newSAXParser();
final XMLReader reader = sp.getXMLReader();
final MovieXmlHandler handler = new MovieXmlHandler();
reader.setContentHandler(handler);
reader.parse(new InputSource(sourceUrl.openStream()));
final ArrayList<Movie> movies = handler.getRecords();
如果您有 xml 数据可从in:InputStream
, then
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
final MovieXmlHandler handler = new MovieXmlHandler();
parser.parse(in, handler);
final ArrayList<Movie> movies = handler.getRecords();
如果这不是您想要的,请告诉我们。