简体   繁体   English

从XML样式的String中删除无效字符

[英]remove invalid character from a String in XML style

I'm create a web service which receive string and convert it to XML. 我正在创建一个Web服务,它接收字符串并将其转换为XML。 The XML creation is done through java dom4j. XML创建是通过java dom4j完成的。 The string's format is like: 字符串的格式如下:

<form01><Textbox1 id="Textbox1" dataType="java.lang.String" perDataProId="">Site1</Textbox1><Textbox2 id="Textbox2" dataType="java.lang.String" perDataProId="">Site1</Textbox2></form01>

To avoid XML invalid character I think I can use a StringReader to read string and remove &,but I wonder how to remove < and >?For example if the input string is 为了避免XML无效字符,我想我可以使用StringReader来读取字符串并删除&,但我想知道如何删除<和>?例如,如果输入字符串是

<form01><Textbox1 id="Textbox1" dataType="java.lang.String" perDataProId="">Site<1</Textbox1><Textbox2 id="Textbox2" dataType="java.lang.String" perDataProId="">Site1</Textbox2></form01>

how to remove the '<' in "Site<1" while keep others like ? 如何删除“Site <1”中的“<”,同时保持其他人喜欢? Any suggestion?Thx. 有什么建议吗?Thx。

Normally the XML APIs will take care when setting the text in an XML DOM, or retrieving it later. 通常,XML API在XML DOM中设置文本或稍后检索时会很小心。

Some characters will be converted to an XML entity: < > " ' & . 某些字符将转换为XML实体: < > " ' &

There are also some Characters not allowed in some XML versions, like \ . 某些XML版本中也不允许使用某些字符 ,例如\

apache.commons.lang has a StringEscapeUtils.escapeXML if you need to do the conversion yourself as pure text. apache.commons.lang有一个StringEscapeUtils.escapeXML如果你需要自己做纯文本转换。

You could use the following RegExp: 您可以使用以下RegExp:

  public static void main(String[] args)
  {
    String str = "<form01><Textbox1 id=\"Textbox1\" dataType=\"java.lang.String\" perDataProId=\"\">Site<1</Textbox1><Textbox2 id=\"Textbox2\" dataType=\"java.lang.String\" perDataProId=\"\">Site1>a</Textbox2><Textbox3 id=\"Textbox2\" dataType=\"java.lang.String\" perDataProId=\"\">Site1&</Textbox3></form01>";
    System.out.println(str.replaceAll("(>[^<>]*)<([^<>]*<\\/)", "$1&lt;$2"));
    System.out.println(str.replaceAll("(>[^<>]*)>([^<>]*<\\/)", "$1&gt;$2"));
    System.out.println(str.replaceAll("(>[^<>]*)\\&([^<>]*<\\/)", "$1&amp;$2"));
  }

Results: 结果:

<form01><Textbox1 id="Textbox1" dataType="java.lang.String" perDataProId="">Site&lt;1</Textbox1><Textbox2 id="Textbox2" dataType="java.lang.String" perDataProId="">Site1>a</Textbox2><Textbox3 id="Textbox2" dataType="java.lang.String" perDataProId="">Site1&</Textbox3></form01>
<form01><Textbox1 id="Textbox1" dataType="java.lang.String" perDataProId="">Site<1</Textbox1><Textbox2 id="Textbox2" dataType="java.lang.String" perDataProId="">Site1&gt;a</Textbox2><Textbox3 id="Textbox2" dataType="java.lang.String" perDataProId="">Site1&</Textbox3></form01>
<form01><Textbox1 id="Textbox1" dataType="java.lang.String" perDataProId="">Site<1</Textbox1><Textbox2 id="Textbox2" dataType="java.lang.String" perDataProId="">Site1>a</Textbox2><Textbox3 id="Textbox2" dataType="java.lang.String" perDataProId="">Site1&amp;</Textbox3></form01>

In any case, I would consider using Guava HtmlEscapers . 无论如何,我会考虑使用Guava HtmlEscapers The RegExp for this case are a fragile overhead. 这种情况下的RegExp是一个脆弱的开销。

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

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