解析器错误

发布于 2024-10-04 08:18:50 字数 1484 浏览 1 评论 0原文

我想解析这个 xml 并获取标签之间的结果...但我无法获取结果我的 xml 是

true<结果>成功 处理程序

public class MyXmlContentHandler extends DefaultHandler {
    String result;
    private String currentNode;
    private String currentValue = null;
    public String getFavicon() {
        return result;
    }
    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {


        if (localName.equalsIgnoreCase("result")) {
            //offerList = new BFOfferList();
            this.result = new String();

        }
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {


        if (localName.equalsIgnoreCase("result")) {
            result = localName;

        } 
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {

         String value = new String(ch, start, length);

         if (currentNode.equals("result")){
            result = value;
            return;
         }


}



}

需要任何更改

I want to parse this xml and get the result between the tag... but i cant get the result my xml is

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><loginResponse xmlns="https://comm1.get.com/"><loginResult>true</loginResult><result>success</result></loginResponse></soap:Body></soap:Envelope>

The handler

public class MyXmlContentHandler extends DefaultHandler {
    String result;
    private String currentNode;
    private String currentValue = null;
    public String getFavicon() {
        return result;
    }
    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {


        if (localName.equalsIgnoreCase("result")) {
            //offerList = new BFOfferList();
            this.result = new String();

        }
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {


        if (localName.equalsIgnoreCase("result")) {
            result = localName;

        } 
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {

         String value = new String(ch, start, length);

         if (currentNode.equals("result")){
            result = value;
            return;
         }


}



}

Any changes needed

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

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

发布评论

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

评论(1

盛夏尉蓝 2024-10-11 08:18:50

当您找到要查找的开始标记时,将调用一次或多次“字符”。您必须收集数据而不是覆盖它。更改

if (currentNode.equals("result")){
        result = value;
        return;
     }

if (currentNode.equals("result")){
        result += value;
        return;
     }

或使用 StringBuilder 来完成。此外,您应该删除它,它似乎会覆盖您的结果字符串:

result = localName;

编辑

public class MyXmlContentHandler extends DefaultHandler {

private String result = "";
private String currentNode;

public String getFavicon() {
    return result;
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    currentNode = localName;
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
    currentNode = null;
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {

    String value = new String(ch, start, length);

    if ("result".equals(currentNode)){
        result += value;
    }
}
}

When you found the start tag you are looking for, "characters" is called one or more times. You have to collect the data not overwrite it. Change

if (currentNode.equals("result")){
        result = value;
        return;
     }

to

if (currentNode.equals("result")){
        result += value;
        return;
     }

Or use StringBuilder to do it. Furthermore, you should remove this, it seems to overwrite your result String:

result = localName;

EDIT:

public class MyXmlContentHandler extends DefaultHandler {

private String result = "";
private String currentNode;

public String getFavicon() {
    return result;
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    currentNode = localName;
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
    currentNode = null;
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {

    String value = new String(ch, start, length);

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