如何包含&,<,> XML属性值中的等 [英] How do I include &, <, > etc in XML attribute values

查看:161
本文介绍了如何包含&,<,> XML属性值中的等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个XML文件,该文件将用于存储Java程序的结构.我能够成功解析Java程序并根据需要创建标签.当我尝试将源代码包含在标签中时,就会出现问题,因为Java源代码可能会使用大量的实体引用和保留字符,例如&<>&.我无法创建有效的XML.

I want to create an XML file which will be used to store the structure of a Java program. I am able to successfully parse the Java program and create the tags as required. The problem arises when I try to include the source code inside my tags, since Java source code may use a vast number of entity reference and reserved characters like &, < ,> , &. I am not able to create a valid XML.

我的XML应该像这样:

My XML should go like this:

<?xml version="1.0"?>
<prg name="prg_name">
  <class name= "class_name>
    <parent>parent class</parent>
      <interface>Interface name</interface>
.
.
.
      <method name= "method_name">
        <statement>the ordinary java statement</statement>
        <if condition="Conditional Expression">
          <statement> true statements </statement>
        </if>
        <else>
          <statement> false statements </statement>
        </else>
        <statement> usual control statements </statement>
 .
 .
 .
      </method>
    </class>
 .
 .
 .
 </prg>

像这样,但是问题是if或其他语句的条件表达式中有很多&或其他保留符号,这妨碍了XML的验证.由于所有这些数据(源代码)都是由用户提供的,因此我对此几乎没有控制权.转义字符会花费很多时间.

Like this, but the problem is conditional expressions of if or other statements have a lot of & or other reserved symbols in them which prevents XML from getting validated. Since all this data (source code) is given by the user I have little control over it. Escaping the characters will be very costly in terms of time.

我可以使用CDATA来转义元素文本,但不能用于包含条件表达式的属性值.我正在使用 Antlr Java语法来解析Java程序并获取标记的属性和内容.那么,还有其他解决方法吗?

I can use CDATA to escape the element text but it can not be used for attribute values containing conditional expressions. I am using Antlr Java grammar to parse the Java program and getting the attributes and content for the tags. So is there any other workaround for it?

推荐答案

您将不得不逃脱

" to  &quot;
' to  &apos;
< to  &lt;
> to  &gt;
& to  &amp;

用于xml.

这篇关于如何包含&amp;,&lt;,&gt; XML属性值中的等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆