有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java检测文本内容是否具有CDATA

我有两个用于获取应用程序描述的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


共 (2) 个答案

  1. # 1 楼答案

    如何解析XML

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

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

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

    问候, 麦克斯

  2. # 2 楼答案

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

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

    因此,您的测试可能只是“文本内容是否包含<字符?”