如何解决我的应用程序中的解析器错误?

发布于 2024-12-25 01:49:29 字数 1580 浏览 2 评论 0原文

我成功解析了我的 xml 文件,但现在我得到了 null 值。所以我不知道我在编码中犯了什么错误。我想在屏幕上显示我的字符串值。现在我试图以文本视图格式显示该字符串值,但我得到空值......

我的 xml 文件:

<Mobiles>
<Mobile>
<Phone>Nokia 1108</Phone>
<Network>GSM 900/1800 MHz</Network>
<Size>106x46x20 mm</Size>
<Ringtones>mono</Ringtones>
<SMS>yes</SMS>
<MMS>no</MMS>
<Email>no</Email>
<InstantMessaging >no</InstantMessaging>
</Mobile>

<Mobile>
<Phone>Nokia 1109</Phone>
<Network>GSM 900/1800 MHz</Network>
<Size>106x46x20 mm</Size>
<Ringtones>mono</Ringtones>
<SMS>yes</SMS>
<MMS>no</MMS>
<Email>no</Email>
<InstantMessaging >no</InstantMessaging>
</Mobile>

<Mobile>
<Phone>Nokia 1110</Phone>
<Network>GSM 900/1800 MHz</Network>
<Size>106x46x20 mm</Size>
<Ringtones>mono</Ringtones>
<SMS>yes</SMS>
<MMS>no</MMS>
<Email>no</Email>
<InstantMessaging >no</InstantMessaging>
</Mobile>

<Mobile>
<Phone>Nokia 1111</Phone>
<Network>GSM 900/1800 MHz</Network>
<Size>106x46x20 mm</Size>
<Ringtones>mono</Ringtones>
<SMS>yes</SMS>
<MMS>no</MMS>
<Email>no</Email>
<InstantMessaging >no</InstantMessaging>
</Mobile>
</Mobiles>

输出: 在此处输入图像描述

I parsed my xml file successfully, but now I am getting null value. so what mistake I make in my coding I don't know. I want to display my string value in my screen. Now I am trying to display that string value in text view format, but I am getting null value......

my xml file:

<Mobiles>
<Mobile>
<Phone>Nokia 1108</Phone>
<Network>GSM 900/1800 MHz</Network>
<Size>106x46x20 mm</Size>
<Ringtones>mono</Ringtones>
<SMS>yes</SMS>
<MMS>no</MMS>
<Email>no</Email>
<InstantMessaging >no</InstantMessaging>
</Mobile>

<Mobile>
<Phone>Nokia 1109</Phone>
<Network>GSM 900/1800 MHz</Network>
<Size>106x46x20 mm</Size>
<Ringtones>mono</Ringtones>
<SMS>yes</SMS>
<MMS>no</MMS>
<Email>no</Email>
<InstantMessaging >no</InstantMessaging>
</Mobile>

<Mobile>
<Phone>Nokia 1110</Phone>
<Network>GSM 900/1800 MHz</Network>
<Size>106x46x20 mm</Size>
<Ringtones>mono</Ringtones>
<SMS>yes</SMS>
<MMS>no</MMS>
<Email>no</Email>
<InstantMessaging >no</InstantMessaging>
</Mobile>

<Mobile>
<Phone>Nokia 1111</Phone>
<Network>GSM 900/1800 MHz</Network>
<Size>106x46x20 mm</Size>
<Ringtones>mono</Ringtones>
<SMS>yes</SMS>
<MMS>no</MMS>
<Email>no</Email>
<InstantMessaging >no</InstantMessaging>
</Mobile>
</Mobiles>

output:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

埖埖迣鎅 2025-01-01 01:49:29

只需简单浏览一下您的代码,我会说问题在于您的 in_Mobiles 变量上的状态转换。从文档的开头到结尾,它始终为 true

因此,在 characters(char[], int, int) 方法中,第一个条件分支将消耗所有字符:

if (this.in_Mobiles) {
    myParsedExampleDataSet.setMobiles(new String(ch, start, length));

在使用 in_Mobile 时会重复相同的行为,其中如果你解决了第一个问题,就会成为下一个罪魁祸首。

编辑:
嗯,总的来说,你的解析器实现有点不稳定。尝试这样的事情:

首先,你的 ParsedExampleDataSet 有点不对劲。

将其转换为 Mobile 对象列表,如下所示:

public class ParsedExampleDataSet extends ArrayList<Mobile>{
}

接下来,创建一个名为 Mobile 的 bean 类,如下所示:

class Mobile {
    private String Phone;
    private String Network;
    private String Size;
    private String Ringtones;
    private boolean SMS;
    private boolean MMS;
    private boolean Email;
    private boolean InstantMessaging;
    public String getPhone() {
        return Phone;
    }
    public void setPhone(String phone) {
        Phone = phone;
    }
    public String getNetwork() {
        return Network;
    }
    public void setNetwork(String network) {
        Network = network;
    }
    public String getSize() {
        return Size;
    }
    public void setSize(String size) {
        Size = size;
    }
    public String getRingtones() {
        return Ringtones;
    }
    public void setRingtones(String ringtones) {
        Ringtones = ringtones;
    }
    public boolean isSMS() {
        return SMS;
    }
    public void setSMS(boolean sMS) {
        SMS = sMS;
    }
    public boolean isMMS() {
        return MMS;
    }
    public void setMMS(boolean mMS) {
        MMS = mMS;
    }
    public boolean isEmail() {
        return Email;
    }
    public void setEmail(boolean email) {
        Email = email;
    }
    public boolean isInstantMessaging() {
        return InstantMessaging;
    }
    public void setInstantMessaging(boolean instantMessaging) {
        InstantMessaging = instantMessaging;
    }
}

最后,需要重新设计您的 DefaultHandler 子类。像这样的东西应该有效。

class ExampleHandler extends DefaultHandler {
    private ParsedExampleDataSet Mobiles;
    private Mobile CurrentMobile;
    private StringBuilder Characters;
    public ParsedExampleDataSet getParsedExampleDataSet() {
        return Mobiles;
    }
    public void startDocument() throws SAXException {
        Mobiles = new ParsedExampleDataSet();
    }
    public void startElement(String namespaceUri, String localName, String qName, Attributes atts)
            throws SAXException {
        String name = localName.equals("") ? qName : localName;
        if ("Mobile".equals(name)) {
            CurrentMobile = new Mobile();
        }
        // Empty accumulated characters
        Characters = null;
    }
    public void characters(char[] ch, int offset, int length) throws SAXException {
        if (Characters == null) {
            Characters = new StringBuilder(length);
        }
        Characters.append(ch, offset, length);
    }
    public void endElement(String namespaceUri, String localName, String qName) throws SAXException {
        String name = localName.equals("") ? qName : localName;
        if ("Mobile".equals(name)) {
            Mobiles.add(CurrentMobile);
            CurrentMobile = null;
        } else if (CurrentMobile != null && Characters != null){
            String value = Characters.toString();
            if ("Phone".equals(name)) {
                CurrentMobile.setPhone(value);
            } else if ("Network".equals(name)) {
                CurrentMobile.setNetwork(value);
            } else if ("Size".equals(name)) {
                CurrentMobile.setSize(value);
            } else if ("Ringtones".equals(name)) {
                CurrentMobile.setRingtones(value);
            } else {
                boolean yes = "yes".equalsIgnoreCase(value.trim());
                if ("SMS".equals(name)) {
                    CurrentMobile.setSMS(yes);
                } else if ("MMS".equals(name)) {
                    CurrentMobile.setMMS(yes);
                } else if ("Email".equals(name)) {
                    CurrentMobile.setEmail(yes);
                } else if ("InstantMessaging".equals(name)) {
                    CurrentMobile.setInstantMessaging(yes);
                }
            }
        }
    }
}

并且,像这样运行它应该会产生结果:

SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
ExampleHandler handler = new ExampleHandler();
InputSource is = new InputSource(/* your XML goes here as an inputstream or reader*/);
parser.parse(is, handler);
ParsedExampleDataSet mobiles = handler.getParsedExampleDataSet();
for (Mobile mobile : mobiles) {
    System.out.println(mobile.getPhone());
}

Just briefly glancing at your code I'd say that the issue is the state transitions on your in_Mobiles variable. It will always be true from the start of the document to the end.

In your characters(char[], int, int) method, the very first conditional branch will thus consume all characters:

if (this.in_Mobiles) {
    myParsedExampleDataSet.setMobiles(new String(ch, start, length));

The same behavior repeats in the use of in_Mobile, which if you fix the first one, will be the next culprit.

Edit:
Well, overall your parser implementation is kind of wonky. Try something like this instead:

First off, your ParsedExampleDataSet is a bit off.

Turn it into a List of Mobile objects instead, like this:

public class ParsedExampleDataSet extends ArrayList<Mobile>{
}

Next, make a bean class named Mobile, like this:

class Mobile {
    private String Phone;
    private String Network;
    private String Size;
    private String Ringtones;
    private boolean SMS;
    private boolean MMS;
    private boolean Email;
    private boolean InstantMessaging;
    public String getPhone() {
        return Phone;
    }
    public void setPhone(String phone) {
        Phone = phone;
    }
    public String getNetwork() {
        return Network;
    }
    public void setNetwork(String network) {
        Network = network;
    }
    public String getSize() {
        return Size;
    }
    public void setSize(String size) {
        Size = size;
    }
    public String getRingtones() {
        return Ringtones;
    }
    public void setRingtones(String ringtones) {
        Ringtones = ringtones;
    }
    public boolean isSMS() {
        return SMS;
    }
    public void setSMS(boolean sMS) {
        SMS = sMS;
    }
    public boolean isMMS() {
        return MMS;
    }
    public void setMMS(boolean mMS) {
        MMS = mMS;
    }
    public boolean isEmail() {
        return Email;
    }
    public void setEmail(boolean email) {
        Email = email;
    }
    public boolean isInstantMessaging() {
        return InstantMessaging;
    }
    public void setInstantMessaging(boolean instantMessaging) {
        InstantMessaging = instantMessaging;
    }
}

Finally, your DefaultHandler subclass needs to be reworked. Something like this ought to work.

class ExampleHandler extends DefaultHandler {
    private ParsedExampleDataSet Mobiles;
    private Mobile CurrentMobile;
    private StringBuilder Characters;
    public ParsedExampleDataSet getParsedExampleDataSet() {
        return Mobiles;
    }
    public void startDocument() throws SAXException {
        Mobiles = new ParsedExampleDataSet();
    }
    public void startElement(String namespaceUri, String localName, String qName, Attributes atts)
            throws SAXException {
        String name = localName.equals("") ? qName : localName;
        if ("Mobile".equals(name)) {
            CurrentMobile = new Mobile();
        }
        // Empty accumulated characters
        Characters = null;
    }
    public void characters(char[] ch, int offset, int length) throws SAXException {
        if (Characters == null) {
            Characters = new StringBuilder(length);
        }
        Characters.append(ch, offset, length);
    }
    public void endElement(String namespaceUri, String localName, String qName) throws SAXException {
        String name = localName.equals("") ? qName : localName;
        if ("Mobile".equals(name)) {
            Mobiles.add(CurrentMobile);
            CurrentMobile = null;
        } else if (CurrentMobile != null && Characters != null){
            String value = Characters.toString();
            if ("Phone".equals(name)) {
                CurrentMobile.setPhone(value);
            } else if ("Network".equals(name)) {
                CurrentMobile.setNetwork(value);
            } else if ("Size".equals(name)) {
                CurrentMobile.setSize(value);
            } else if ("Ringtones".equals(name)) {
                CurrentMobile.setRingtones(value);
            } else {
                boolean yes = "yes".equalsIgnoreCase(value.trim());
                if ("SMS".equals(name)) {
                    CurrentMobile.setSMS(yes);
                } else if ("MMS".equals(name)) {
                    CurrentMobile.setMMS(yes);
                } else if ("Email".equals(name)) {
                    CurrentMobile.setEmail(yes);
                } else if ("InstantMessaging".equals(name)) {
                    CurrentMobile.setInstantMessaging(yes);
                }
            }
        }
    }
}

And, just running it like this should produce a result:

SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
ExampleHandler handler = new ExampleHandler();
InputSource is = new InputSource(/* your XML goes here as an inputstream or reader*/);
parser.parse(is, handler);
ParsedExampleDataSet mobiles = handler.getParsedExampleDataSet();
for (Mobile mobile : mobiles) {
    System.out.println(mobile.getPhone());
}
一口甜 2025-01-01 01:49:29

为什么 endElement 方法中有 true ?

 if (localName.equals("Mobiles")) {
                this.in_Mobiles = true;

这可能会导致始终覆盖手机并且无法设置正确的字段。

why do you have true in endElement method?

 if (localName.equals("Mobiles")) {
                this.in_Mobiles = true;

this could leads to always override mobiles and not setting correct field.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文