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 に答える