简体   繁体   English

解析xml特殊字符问题

[英]Parsing xml special chars issue

I'm parsing an XML got from webservice using SAX . 我正在使用SAX解析从webservice获得的XML。

One of the fields is a link, like the following 字段之一是链接,如下所示

<link_site>
   http://www.ownhosting.com/webservice_332.asp?id_user=21395&amp;id_parent=33943
</link_site>

I have to get this link and save it, but it is saved like so: id_parent=33943 . 我必须获取此链接并将其保存,但是它的保存方式如下: id_parent=33943

Parser snippet: 解析器代码段:

//inside method startElement
else if(localName.equals("link_site")){
    this.in_link=true;
}
...
//inside method endElement
else if(localName.equals("link_site"){
     this.in_link=false;
}

Then, I get the content 然后,我得到了内容

else if(this.in_link){
    xmlparsing.setOrderLink(count, Html.fromHtml(new String(ch, start, length)).toString());
}//I get it and put in a HashMap<Integer,String>

I know that this issue is due to the special characters encoding. 我知道此问题是由于特殊字符编码引起的。

What can I do? 我能做什么?

& makes parser to split the line and make several calls to characters() method. &使解析器拆分行并多次调用character()方法。 You need to concatinate the chunks. 您需要隐藏这些块。 Something like this 像这样

    SAXParserFactory.newInstance().newSAXParser()
            .parse(new File("1.xml"), new DefaultHandler() {
                String url;
                String element;

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

                @Override
                public void characters(char[] ch, int start, int length) throws SAXException {
                    if (element.equals("link_site")) {
                        url += new String(ch, start, length); 
                    }
                }

                @Override
                public void endElement(String uri, String localName, String qName)
                        throws SAXException {
                    if (element.equals("link_site")) {
                        System.out.println(url.trim());
                        element = "";
                    }
                }
            });

prints 版画

http://www.ownhosting.com/webservice_332.asp?id_user=21395&id_parent=33943

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM