本文介绍了为什么DOM库中删除逃出特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在XML此内容:

 <地方>
   <&地名GT; @#$%&放大器; * / _'() - +;!?< /地名>
< /位与GT;

这是正确的,当我查看网页源代码

 <地方>
    <&地名GT; @#$%&放大器;放大器; * / _&安培; QUOT;!?() - +;< /地名>
< /位

我用org.w3c.dom.Document中,org.w3c.dom.Element中,...获取内容地名。问题是DOM库中删除转义特殊字符。它显示了!@#$%Android中logcat中。为什么?如何解决呢?

这是我的code的一部分,我使用节点:: getNodeValue摆脱以上​​的XML值:

 公共静态文档getDocument(最后弦乐XML){
        文档DOC = NULL;
        最终的DocumentBuilderFactory DBF = DocumentBuilderFactory.newInstance();
            尝试{
            最终的DocumentBuilder DB = dbf.newDocumentBuilder();
            最后的InputSource是=新的InputSource();
            is.setCharacterStream(新StringReader(XML));
            DOC = db.parse(是);            }赶上(最终的ParserConfigurationException E){
                的System.out.println(XML解析错误:+ e.getMessage());
                返回null;
            }赶上(最终的SAXException E){
                的System.out.println(错的XML文件的结构:+ e.getMessage());
                返回null;
            }赶上(最终IOException异常五){
                的System.out.println(I / O exeption:+ e.getMessage());
                返回null;
            }
            返回文档;
    }    私人静态字符串请求(){
        串线= NULL;
        尝试{
            最后DefaultHttpClient的HttpClient =新DefaultHttpClient();
            最终HTTPGET HTTPGET =新HTTPGET(http://api-url.com);
            最终的Htt presponse HTT presponse = httpClient.execute(HTTPGET);
            最后HttpEntity httpEntity = HTT presponse.getEntity();
            行= EntityUtils.toString(httpEntity);        }赶上(最终UnsupportedEncodingException五){
            行=<结果状态= \\错误\\><味精>不能连接到服务器16; /味精>< /结果>,
        }赶上(最终MalformedURLException的E){
            行=<结果状态= \\错误\\><味精>不能连接到服务器16; /味精>< /结果>,
        }赶上(最终IOException异常五){
            行=<结果状态= \\错误\\><味精>不能连接到服务器16; /味精>< /结果>,
        }
        回线;
    }


解决方案

当你从字符串的文件添加此

  dbf.setCoalescing(真);

其中,DBF是

 的DocumentBuilderFactory DBF = DocumentBuilderFactory.newInstance();

I have this content in XML:

<place>
   <placeName>!@#$%&*?/_"'()-+;</placeName>
</place>

it's correct when I view Page Source

<place>
    <placeName>!@#$%&amp;*?/_&quot;'()-+;</placeName>
</place

I use org.w3c.dom.Document, org.w3c.dom.Element, ... to get the content "placeName". The problem is the DOM library remove the escaped special characters. It shows "!@#$%" in Android logcat. Why? How to fix it?

This is a part of my code, I use Node::getNodeValue to get values from the above XML:

    public static Document getDocument(final String xml) {
        Document doc = null;
        final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            try {
            final DocumentBuilder db = dbf.newDocumentBuilder();
            final InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is);

            } catch (final ParserConfigurationException e) {
                System.out.println("XML parse error: " + e.getMessage());
                return null;
            } catch (final SAXException e) {
                System.out.println("Wrong XML file structure: " + e.getMessage());
                return null;
            } catch (final IOException e) {
                System.out.println("I/O exeption: " + e.getMessage());
                return null;
            }
            return doc;
    }

    private static String request() {
        String line = null;
        try {
            final DefaultHttpClient httpClient = new DefaultHttpClient();
            final HttpGet httpGet = new HttpGet("http://api-url.com");
            final HttpResponse httpResponse = httpClient.execute(httpGet);
            final HttpEntity httpEntity = httpResponse.getEntity();
            line = EntityUtils.toString(httpEntity);

        } catch (final UnsupportedEncodingException e) {
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
        } catch (final MalformedURLException e) {
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
        } catch (final IOException e) {
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
        }
        return line;
    }
解决方案

add this when you get document from string

  dbf.setCoalescing(true); 

where dbf is

 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

这篇关于为什么DOM库中删除逃出特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 11:05