【问题标题】:Detect whether the text content has CDATA检测文本内容是否有CDATA
【发布时间】:2012-02-18 10:09:22
【问题描述】:

我有两个用于获取应用程序描述的 api 和一个通用 UI。我需要检查 Java 中的描述是否带有CDATA 标签。

例如,一个应用具有以下描述:

"<![CDATA[<p>What is Skype?<br />Skype is software that enables the world's     
conversations. Millions of individuals and businesses use Skype to make free video and voice    
calls, send instant messages and share files with other Skype users. Everyday, people also    
use Skype to make low-cost calls to landlines and mobiles.</p>]]>"

另外一个应用有如下描述

Run with your fingers as fast as you can to try and get to the top of the leader board. This    
 game gets even better with friends, Once people see you playing they will want to have a go    
 and try to beat your fastest time. Tip: Take long strides on the screen to get maximum     
distance per step,     
<a href=https://abc.defgh.ij.kl/apps/wap/shopping/shopping/freshima-supermarket/freshima-supermarket/web/>WAP URL</a>

如何区分这两种描述?有没有办法在Java中检测描述是否带有CDATA

【问题讨论】:

  • 为什么重要?它要么使用 CDATA,要么使用实体(或者在需要任何一个的地方都没有字符)。无论您使用什么 XML 库,都应该透明地处理它。

标签: java xml cdata


【解决方案1】:

你是如何解析你的 XML 的?

如果您使用 StAX,则可以获取您在信息流中遇到的当前事件,可能是 XMLStreamConstants.CHARACTERSXMLStreamConstants.CDATA

如果您得到一个Node 对象(例如通过XPathAPI),该对象将为您提供一个getNodeType() 方法。 Node 也有 Node.TEXT_NODE 的常量和 Node.CDATA_SECTION_NODE.

更多信息将有助于回答您的问题。

问候, 最大

【讨论】:

    【解决方案2】:

    您不应区别对待以下两个示例,因为就 XML 而言,它们只是转义相同内容的不同方式:

    <a><![CDATA[<xyz/>]]></a>
    
    <a>&lt;xyz/&gt;</a>
    

    所以也许您的测试只是“文本内容是否包含&lt; 字符?”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-04
      • 2011-08-11
      • 2014-10-22
      • 1970-01-01
      • 2017-11-24
      相关资源
      最近更新 更多