为什么DOM库删除转义的特殊字符?

问题描述:

我在这个XML内容:为什么DOM库删除转义的特殊字符?

<place> 
    <placeName>[email protected]#$%&*?/_"'()-+;</placeName> 
</place> 

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

<place> 
    <placeName>[email protected]#$%&amp;*?/_&quot;'()-+;</placeName> 
</place 

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

这是我的代码的一部分,我使用节点:: getNodeValue从上面的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; 
    } 

添加此当你从字符串

dbf.setCoalescing(true); 

其中DBF文件获取是

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
+0

它的工作原理,但你能解释为什么?因为这个问题,我拉了很多头发。 – Emerald214 2012-07-30 08:57:24

+0

bcz我们需要将cdata节点转换为文本节点请参阅示例http://www.roseindia.net/xml/converting-cdata-nodes-into-text.shtml – Khan 2012-07-30 09:11:41

+0

阅读它,但仍然没有得到它@ _ @什么是聚结? – Emerald214 2012-07-30 09:20:22