简体   繁体   English

如何使用Java解析XML中任何标签的CDATA部分中存在的元素标签

[英]How to parse element tags present in the CDATA section of any tag in XML using java

I am currently trying to parse xml file using java but while parsing i am unable to parse the tags present inside the CDATA section of any element tag. 我目前正在尝试使用Java解析xml文件,但是在解析时我无法解析任何元素标签的CDATA部分中存在的标签。 The whole data inside the CDATA section is being parsed as text while parsing using DOM or SAX. 使用DOM或SAX进行解析时,CDATA部分中的所有数据都被解析为文本。

please suggest any way without any hard coding. 请提出任何没有任何硬编码的方法。

<Evaluation>
<![CDATA[<evaluation>
<interview ratingtype="0"/>
<item description="Ability to communicate well with others verbally and in writing:" hasrating="True" hascomments="False" /> ............ </item>
 </evaluation> ]]
</Evaluation>

CDATA sections are text nodes. CDATA节是文本节点。 So the parser is correct reading it as a single string. 因此,解析器将其作为单个字符串读取是正确的。 CDATA sections mean that the parser will not do any decoding of entities. CDATA部分意味着解析器将不对实体进行任何解码。 You can read more about it in the specification . 您可以在规范中阅读有关它的更多信息。

If you want to treat the contents of a CDATA section as an XML document or fragment you need to do this manually - in other words: parse it separately. 如果要将CDATA节的内容视为XML文档或片段,则需要手动进行操作-换句话说:分别对其进行分析。

CDATA represents text nodes, as mentioned above. 如上所述,CDATA表示文本节点。 It won't make sense to keep tags inside a CDATA section. 将标签保留在CDATA部分中没有任何意义。 CDATA should be used to store plain strings. CDATA应该用于存储纯字符串。

In this case the , and tags won't get parsed as elements by the parser. 在这种情况下,解析器不会将和标记解析为元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM