如何使用http post服务解析xml

时间:2011-03-11 11:49:41

标签: android

伙计们,我想在android中解析一个由HTTPPost方法服务获得的xml。我将xml存储在一个字符串变量中,但我不知道如何解析它。 becoz它是一种字符串格式。任何人都可以为此提供示例代码吗? .............

2 个答案:

答案 0 :(得分:0)

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:用于分析文件XML中内容的类使用者

答案 1 :(得分:0)

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。