简体   繁体   English

如何使用Java从XML文件中删除&?

[英]How to remove & from an XML file using java?

I am reading the input Excel file and in that i have 1 fields for the address.In that field the special character is coming which is (&).While i am going to parse this character (&) and trying to write it to the XML file at that time the character converts into the (&amp). 我正在读取输入的Excel文件,其中我有1个用于输入地址的字段。在该字段中,特殊字符即将出现(&)。而我要解析此字符(&)并尝试将其写入到当时的XML文件将字符转换为(&amp)。

I want to remove the translated character (&amp) from the XML file and wants that it must remain as it was read from the Excel file and writtern as the (&) in the XML file. 我想从XML文件中删除转换后的字符(&amp),并希望它必须保留为从Excel文件中读取的字符,并且必须作为XML文件中的(&)书写。 WWW.GEVERS& converted to WWW.GEVERS&amp if any body has any solution or any other code snippet,which would be appreciated. 如果任何机构有任何解决方案或任何其他代码片段, 请将WWW.GEVERS&转换为WWW.GEVERS&amp ,我们将不胜感激。

Thanks, 谢谢,

Mishal Shah 米沙尔·沙(Mishal Shah)

If your data contains an ampersand -- which apparently it does -- then that ampersand has to be escaped as & 如果您的数据包含“&”号(显然确实包含),则该“&”号必须以& if you put it in a text node in your XML document. 如果将其放在XML文档的文本节点中。

So it sounds like your XML software is behaving correctly. 因此,听起来您的XML软件行为正确。 Why do you think you have to do anything about this? 您为什么认为必须对此做任何事情? I don't think you do. 我不认为你这样做。

@Mishal, see Paul's answer. @Mishal,请参阅Paul的回答。 If you replace the & 如果您更换& with a & , you no longer have a "well-formed" XML document. 使用& ,您将不再具有“格式正确的” XML文档。 You would actually be introducing a syntax error. 您实际上会引入语法错误。

So you just need to read or view your XML document with a program that knows how to render XML, such as Internet Explorer (just make sure your file suffix is .xml). 因此,您只需要使用一个知道如何呈现XML的程序(例如Internet Explorer)来阅读或查看XML文档(只需确保文件后缀为.xml)即可。

Since it will not be a valid xml file anymore, you should handle it as raw text and do a raw replace of & 由于它不再是有效的xml文件,因此您应将其作为原始文本进行处理,并对& -> & ->&

By the way, why don't you want to keep it valid xml ? 顺便说一句,为什么不保留有效的xml?

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

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