问题
I have two api for getting a description of apps and one common UI. I need to check whether the description come with CDATA
tag or not in Java.
For example, one app has the following description :
"<![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>]]>"
And another app has the following description
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>
How can I differentiate there two description? Is there a way to detect whether the description comes with CDATA
or not in Java?
回答1:
How are you parsing your XML?
If you are using StAX, you can get the current event that you encounter in your stream, which might be XMLStreamConstants.CHARACTERS
or XMLStreamConstants.CDATA
.
If you are getting a Node
Object (like for instance via XPathAPI), the Object will offer you a getNodeType()
Method. Also Node
has Constants for Node.TEXT_NODE
and
Node.CDATA_SECTION_NODE
.
More Information would be helpful answering your question.
Regards, Max
回答2:
You should not be treating the following two examples differently, because as far as XML is concerned, they are just different ways of escaping the same content:
<a><![CDATA[<xyz/>]]></a>
<a><xyz/></a>
So perhaps your test is simply "does the text content contain a <
character?".
来源:https://stackoverflow.com/questions/8999089/detect-whether-the-text-content-has-cdata