0

我正在尝试从下面的代码中解析 xml:

XMLParser xmlParser = new XMLParser();
String xml = xmlParser.getXmlFromUrl(URL2);
Document doc = xmlParser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName("item");
Element e = (Element) nl.item(0);

当 URL2 = http://api.androidhive.info/pizza/?format=xml时工作正常 但是当我使用我的服务器时: URL2 = http://udios.bugs3.com/test.xml它位于最后一行:

NodeList nl = doc.getElementsByTagName("item");

除了以下例外:

NullPointerException
Unexpected token (position:TEXT @1:4 in java.io.StringReader@41391770) 

2个xml文件是相同的,我不认为问题出在服务器上,也许是编码问题?如果有人能帮助我,我将不胜感激谢谢

4

2 回答 2

1

我认为此链接已被完整使用...

检查此链接


i have shown with only <name>...</name> tag, do the same with <id>...</id> , <cost>...</cost> and <description>...</description> 

     TextView name[];

try {

     /** Handling XML */
     SAXParserFactory spf = SAXParserFactory.newInstance();
     SAXParser sp = spf.newSAXParser();
     XMLReader xr = sp.getXMLReader();

     /** Send URL to parse XML Tags */
     URL sourceUrl = new URL(URL2);

     /** Create handler to handle XML Tags ( extends DefaultHandler ) */
     MyXMLHandler myXMLHandler = new MyXMLHandler();
     xr.setContentHandler(myXMLHandler);
     xr.parse(new InputSource(sourceUrl.openStream()));

} catch (Exception e) {
     System.out.println("XML Pasing Excpetion = " + e);
}



     /** Get result from MyXMLHandler SitlesList Object */
     sitesList = MyXMLHandler.sitesList;

     /** Assign textview array lenght by arraylist size */
     name = new TextView[sitesList.getName().size()];


     /** Set the result text in textview and add it to layout */
     for (int i = 0; i < sitesList.getName().size(); i++) {
          name[i] = new TextView(this);
          name[i].setText("Name = "+sitesList.getName().get(i));    
     }

         MyXMLHandler.java

        /* This file is used to handle the XML tags. So we need to extends with DefaultHandler.
         we need to override startElement, endElement & characters method .
         startElemnt method called when the tag starts.
         endElemnt method called when the tag ends
         characres method to get characters inside tag.

         */



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

         public class MyXMLHandler extends DefaultHandler {

         Boolean currentElement = false;
         String currentValue = null;
         public static SitesList sitesList = null;

         public static SitesList getSitesList() {
         return sitesList;
         }

         public static void setSitesList(SitesList sitesList) {
         MyXMLHandler.sitesList = sitesList;
         }

         /** Called when tag starts ( ex:- <name>Android</name>
         * -- <name> )*/
         @Override
         public void startElement(String uri, String localName, String qName,
         Attributes attributes) throws SAXException {

         currentElement = true;

         if (localName.equals("menu"))
         {
         /** Start */
         sitesList = new SitesList();
         } 

         }

         /** Called when tag closing ( ex:- <name>Android</name>
         * -- </name> )*/
         @Override
         public void endElement(String uri, String localName, String qName)
         throws SAXException {

         currentElement = false;

         /** set value */
         if (localName.equalsIgnoreCase("name"))
         sitesList.setName(currentValue);


         }

         /** Called to get tag characters ( ex:- <name>Android</name>
         * -- to get Android Character ) */
         @Override
         public void characters(char[] ch, int start, int length)
         throws SAXException {

         if (currentElement) {
         currentValue = new String(ch, start, length);
         currentElement = false;
         }

     }

}


          SitesList.java

          // Contains Getter & Setter Method


          import java.util.ArrayList;

          /** Contains getter and setter method for varialbles */
       public class SitesList {

          /** Variables */
          private ArrayList<String> name = new ArrayList<String>();

          /** In Setter method default it will return arraylist
          * change that to add */

          public ArrayList<String> getName() {
          return name;
          }

          public void setName(String name) {
          this.name.add(name);
          }


       }
于 2012-09-06T12:17:59.357 回答
0

找到解决方法:使用 xml - 用记事本打开 - 另存为 (ANSI)

于 2012-09-05T10:33:33.120 回答