如何从Android中获取XML数据

时间:2016-07-23 08:26:11

标签: android arraylist xml-parsing

我创建了用于从XML格式获取数据的类,但它返回的数据是错误的数倍?实际上我只想要两个标签 en ur 而不是其他标签。 我的服务器数据

<?xml version="1.0" encoding="UTF-8"?>
<words>
   <word wid="373012">
      <en>DOCTOR (Noun)</en>
      <ur>حکیم </ur>
   </word>
   <word wid="356630">
      <en>DOCTOR (Verb)</en>
      <ur>بدلنا </ur>
   </word>
   <word wid="356633">
      <en>DOCTOR (Verb)</en>
      <ur>بدلنا  دھوکا  دینا  ۔</ur>
   </word>
   <word wid="329801">
      <en>DOCTOR</en>
      <ur>ڈاکٹر  معالج  ۔</ur>
   </word>
   <word wid="364114">
      <en>DOCTOR</en>
      <ur>اُستاد </ur>
   </word>
   <word wid="190805">
      <en>DOCTOR (Verb)</en>
      <ur>ڈاکٹر بنانا  ۔</ur>
   </word>
</words>

用于还原列表和XML解析的java代码

try {
        ArrayList<UEDWord> list = null;
        URL URL = new URL(stringURL);
        XMLReader mXMLReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
        mXMLReader.setContentHandler(new MXMLHandler());
        mXMLReader.parse(new InputSource(URL.openStream()));
        list = MXMLHandler.getSearchResults();
        return list;
        } catch (Exception e) {
        return null;
    }

//

public class MXMLHandler extends DefaultHandler {
    public static ArrayList<UEDWord> SearchResults = new ArrayList<UEDWord>();
    String LastEngWord = "";
    String LastUrduWord = "";
    Boolean currentElement = false;
    String currentValue = null;

    public MXMLHandler() {
        SearchResults.clear();
    }

    public static ArrayList<UEDWord> getSearchResults() {
        return SearchResults;
    }

    public void characters(char[] characters, int position, int noOfChar) throws SAXException {
        if (currentElement) {
            currentValue = new String(characters, position, noOfChar);
            currentElement = false;
        }
    }

    public void startElement(String URI, String localname, String qualifiedname, Attributes attributes)
            throws SAXException {
        currentElement = true;
    }

    public void endElement(String URI, String localname, String qualifiedname) throws SAXException {
        currentElement = false;

        if (localname.equalsIgnoreCase("en"))
            LastEngWord = currentValue;
        // do {
        if (localname.equalsIgnoreCase("ur"))
            LastUrduWord = currentValue;
        // return;
        // } while (!localname.equalsIgnoreCase("word"));
        // Log.e(URI, localname + " " + qualifiedname);
        SearchResults.add(new UEDWord(LastEngWord, LastUrduWord));
    }
}

SetterGetter Class

public class UEDWord {
    private String EngWord;
    private String UrduWord;

    UEDWord(String EngWord, String UrduWord) {
        this.EngWord = EngWord;
        this.UrduWord = UrduWord;
    }

    public String getEngWord() {
        return this.EngWord;
    }

    public String getUrduWord() {
        return this.UrduWord;
    }

    public void setEngWord(String EngWord) {
        this.EngWord = EngWord;
    }

    public void setUrduWord(String UrduWord) {
        this.UrduWord = UrduWord;
    }
}

1 个答案:

答案 0 :(得分:1)

以下是我的想法:

您的解析器到达'en'元素。进入“startElement”方法,将“currentElement”设置为true,然后进入加载String的“characters”方法,最后进入“endElement”方法,在该方法中使用String创建UEDWord。 / p>

此时已经出现错误:您正在尝试使用EN和UR作为参数创建UEDWord,但这里只有两个中的一个。在创建和添加Word之前检查两者是否都已设置。

展望未来,您的解析器会查看“ur”元素。阅读内容。创建一个新Word并将其添加到列表中(此时,您创建并添加了2个单词而不是1个单词。)

现在解析器继续查看'/ word',关闭之前的'word'元素。因此调用了endElement方法,但是currentValue从未被重置,因此创建了一个与之前相同的新UEDWord并将其添加到数组中。

最后,如果我的问题正确,你应该有13个多余的单词。

我的建议是检查你在endElement中找到的元素,如果你准备好添加EN和UR字,只添加到列表中。将值添加到列表后,清除currentValue和LastEngWord,LastUrduWord,以便下次检查它们为空。

这应该有效(如评论中所述,未经测试):

public class MXMLHandler extends DefaultHandler {
    public static ArrayList<UEDWord> SearchResults = new ArrayList<UEDWord>();
    String LastEngWord = null;
    String LastUrduWord = null;
    boolean currentElement = false;
    String currentValue = null;

    public MXMLHandler() {
        SearchResults.clear();
    }

    public static ArrayList<UEDWord> getSearchResults() {
        return SearchResults;
    }

    public void characters(char[] characters, int position, int noOfChar) throws SAXException {
        if (currentElement) {
            currentValue = new String(characters, position, noOfChar);
            currentElement = false;
        }
    }

    public void startElement(String URI, String localname, String qualifiedname, Attributes attributes)
            throws SAXException {
        currentElement = true;
    }

    public void endElement(String URI, String localname, String qualifiedname) throws SAXException {
        currentElement = false;


        if (localname.equalsIgnoreCase("en"))
            LastEngWord = currentValue;
        else if (localname.equalsIgnoreCase("ur"))
            LastUrduWord = currentValue;

        if(LastEngWord != null && LastUrdWord != null)
        {
           SearchResults.add(new UEDWord(LastEngWord, LastUrduWord));
           LastEngWord = null;
           LastUrdWord = null;
        }
    }
}