0
夥計們,我想解析一個由HTTPPost方法服務獲得的android中的xml。我將該xml存儲在一個字符串變量中,但我不知道如何解析它。因爲它是一個字符串格式。任何人可以給這個例子代碼? .............如何用http post服務解析xml
夥計們,我想解析一個由HTTPPost方法服務獲得的android中的xml。我將該xml存儲在一個字符串變量中,但我不知道如何解析它。因爲它是一個字符串格式。任何人可以給這個例子代碼? .............如何用http post服務解析xml
private RSSFeed getFeed(String urlToRssFeed)
{
try
{
// setup the url
URL url = new URL(urlToRssFeed);
// create the factory
SAXParserFactory factory = SAXParserFactory.newInstance();
// create a parser
SAXParser parser = factory.newSAXParser();
// create the reader (scanner)
XMLReader xmlreader = parser.getXMLReader();
// instantiate our handler
RSSHandler theRssHandler = new RSSHandler();
// assign our handler
xmlreader.setContentHandler(theRssHandler);
// get our data via the url class
InputSource is = new InputSource(url.openStream());
// perform the synchronous parse
xmlreader.parse(is);
// get the results - should be a fully populated RSSFeed instance, or null on error
return theRssHandler.getFeed();
}
catch (Exception ee)
{
// if we have a problem, simply return null
return null;
}
}
RSSHandler:類utiliser到分析儀在XML文件的
DBF = DocumentBuilderFactory.newInstance();
DB = DBF.newDocumentBuilder();
url_val = new URL("YOUR LINK");
dom = DB.parse(url_val.openConnection().getInputStream());
elt = dom.getDocumentElement();
NodeList items = elt.getElementsByTagName("YOUR Root Node");
for (int i = 0; i < items.getLength(); i++) {
Node item = items.item(i);
NodeList properties = item.getChildNodes();
for (int j = 0; j < properties.getLength(); j++) {
Node property = properties.item(j);
String name = property.getNodeName();
if (name.equalsIgnoreCase("Tag Name")) {
cityTitle = property.getFirstChild().getNodeValue(); }}
內容試試這個方法來解析您的XML。