【问题标题】:XMLResourceParser, I'm not sure how to read the XML that I have [closed]XMLResourceParser,我不知道如何阅读我已经 [关闭]
【发布时间】:2013-04-01 21:46:35
【问题描述】:

这是我的 XML 文件的示例:

<?xml version="1.0" encoding="utf-8"?>
<assurances>
<assurance hasReference="true">
    <message>You're Awesome!</message>
    <reference>Genesis 1:26</reference>
</assurance>
<assurance hasReference="true">
    <message>Your Wonderfull!</message>
    <reference>Genesis 1:26</reference>
</assurance>
</assurances>

我正在使用这样的代码来尝试检索它:

int eventType = -1;
    while(eventType != XmlResourceParser.END_DOCUMENT)
    {
        XmlResourceParser assurances = getResources().getXml(R.xml.assurances); 
        String name = assurances.getText();
        Log.d(TAG, name);

        try {
            if (assurances.getEventType() == XmlResourceParser.START_TAG) {
                String s = assurances.getName();

                if (s.equals("assurance")) {
                    String strMessage = assurances.getAttributeValue(null, "message");
                    String strReference = assurances.getAttributeValue(null, "reference");

                    Log.d(TAG, strMessage);
                    Log.d(TAG, strReference);
                }
            }
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

它没有获取数据,我不确定从这里去哪里。

【问题讨论】:

    标签: java android xml


    【解决方案1】:

    您不需要使用 getAttributeValue 方法,而是需要使用 getText() 方法。我已经对您的代码进行了一些更改,所以如果您使用下面的代码,那么它会正常工作:

    int eventType = -1;
    while(eventType != XmlResourceParser.END_DOCUMENT)
    {
        XmlResourceParser assurances = getResources().getXml(R.xml.assurances); 
        String name = assurances.getText();
        Log.d(TAG, name);
    
        try {
            if (assurances.getEventType() == XmlResourceParser.START_TAG) {
                String s = assurances.getName();
    
                if (s.equals("assurance")) {
                    assurances.next();   /// moving to the next node
                    if(assurances.getName() != null && assurances.getName().equalsIgnoreCase("message")){
                        String strMessage = assurances.getText();  ///to get value getText() method should be used
                        assurances.next();   ///jumping on to the next node
                    String strReference = assurances.getText();  ////similar to above
                }
    
                    Log.d(TAG, strMessage);
                    Log.d(TAG, strReference);
                }
            }
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    

    【讨论】:

    • 谢谢你,我确实用那个代码让它工作了,不过我最终做了一个 sqlite 数据库。
    • 你会如何选择我在 stackoverflow 上的问题。这是链接。 stackoverflow.com/questions/53570140/…
    【解决方案2】:

    希望这对您有所帮助。 问候 S

    public static void main(String[] args) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new File("c:\\_work\\test.xml"));
    
            //get nodes by name
            NodeList nl = doc.getElementsByTagName("assurance");
            int nbrOfElements = nl.getLength();
            for (int i = 0; i < nbrOfElements; i++) {
                Element e = (Element) nl.item(i);
                System.out.print(e.getTagName());
                System.out.print(":");
                System.out.println(e.getTextContent());
            }
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-21
      • 1970-01-01
      • 2013-12-30
      • 2012-03-07
      • 2014-03-20
      • 1970-01-01
      相关资源
      最近更新 更多