我想创建一个 XML 文件,用于存储 Java 程序的结构。我能够成功解析 Java 程序并根据需要创建标签。当我尝试将源代码包含在标签中时,问题就出现了,因为 Java 源代码可能使用大量实体引用和保留字符,例如 &
、<
,>
, &
。我无法创建有效的 XML。
我的 XML 应该像这样:
<?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从得到验证。由于所有这些数据(源代码)都是由用户提供的,我对其几乎无法控制。逃离角色会花费非常多的时间。
我可以使用 CDATA 转义元素文本,但它不能用于包含条件表达式的属性值。我正在使用 Antlr Java 语法来解析 Java 程序并获取标签的属性和内容。那么还有其他解决方法吗?
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.
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>
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.
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?
发布评论
评论(2)
你将不得不转义
为 xml。
You will have to escape
for xml.
必须进行转义
在 XML 属性中,如果将属性值括在双引号 (
"
) 中,则,例如,表示带有属性
attr
的标签MyTag
和文本如果 a - 注意:不需要使用
'
来转义'
字符如果将属性值括在单引号 (
'
) 中,则应转义这些字符:并且可以按原样编写
"
。属性文本中的
>
与>
的转义不是必需的,例如"/ >
是格式正确的 XML。In XML attributes you must escape
if you wrap attribute values in double quotes (
"
), e.g.meaning tag
MyTag
with attributeattr
with textIf a<b & b<c then a<c, it's obvious
- note: no need to use'
to escape'
character.If you wrap attribute values in single quotes (
'
) then you should escape these characters:and you can write
"
as is.Escaping of
>
with>
in attribute text is not required, e.g.<a b=">"/>
is well-formed XML.