I have an xml file like this
我有一个像这样的xml文件
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
This is the actual XML file. In our application, we receive this xml as a String.
这是实际的XML文件。在我们的应用程序中,我们将此xml作为String接收。
But somehow, due to hardware problems, the application is receiving the xml like the following.
但不知何故,由于硬件问题,应用程序正在接收xml,如下所示。
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove+++</to>
<from>Jani+</from>
<heading>Reminder++++</heading>
<body>Don't forget me this weekend!</body>
</note>
In my java method I wrote code like this,
在我的java方法中,我编写了这样的代码,
//It will trim all the + in the xml.
if(xml.indexOf("[+]")!=-1)){
xml = xml.replaceAll("[+]","").trim();
}
This gave a result like the following.
这给出了如下结果。
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove </to>
<from>Jani </from>
<heading>Reminder </heading>
<body>Don't forget me this weekend!</body>
</note>
I again modified my code like this.
我再次像这样修改了我的代码。
//It will replace all the + and white spaces in the xml.
if(xml.indexOf("[+]")!=-1)){
xml = xml.replaceAll("[+ ]","").trim();
}
But, this code is giving me a result like the following.
但是,这段代码给了我一个如下结果。
<?xmlversion="1.0"encoding="ISO-8859-1"?>//here I lose the spaces.
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Here I am losing the spaces in the <?xmlversion="1.0"encoding="ISO-8859-1"?>
.
在这里,我将丢失 中的空格。
The other part of the xml body is OK.
xml正文的另一部分没问题。
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
except this <?xmlversion="1.0"encoding="ISO-8859-1"?>.
除了 。
When I am trying to run this in my application, I am getting SAXParcerException: Missing white spaces...etc.
当我试图在我的应用程序中运行它时,我得到SAXParcerException:缺少空格......等等。
How can I write my code exactly to get the file as,
如何准确编写我的代码以获取文件,
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
My application is built on purely Jdk 1.4, web server as tomcat.
我的应用程序基于纯粹的Jdk 1.4,Web服务器构建为tomcat。
3 个解决方案
#1
0
Try with xlm.replaceAll("\\++", "");
to replace one or more +
with nothing at once.
尝试使用xlm.replaceAll(“\\ ++”,“”);一次性取代一个或多个+。
#2
0
It seems you want to remove all "+"
that appear before "</"
,:
您似乎想删除“
xml = xml.replaceAll("\\++<\", "<\");
This won't affect "+"
appearing elsewhere, such as:
这不会影响其他地方出现的“+”,例如:
<tag>Bill + Ted+++</tag>
will become
<tag>Bill + Ted</tag>
#3
0
Maybe you can use
也许你可以使用
if (xml.contains("+")) {
xml = xml.replace("+", "");
}
instead of
xml = xml.replaceAll("[+ ]","").trim();
with jdk 1.4 you can use
用jdk 1.4你可以使用
if (xml.indexOf("+") != -1) {
xml = xml.replace("+", "");
}
#1
0
Try with xlm.replaceAll("\\++", "");
to replace one or more +
with nothing at once.
尝试使用xlm.replaceAll(“\\ ++”,“”);一次性取代一个或多个+。
#2
0
It seems you want to remove all "+"
that appear before "</"
,:
您似乎想删除“
xml = xml.replaceAll("\\++<\", "<\");
This won't affect "+"
appearing elsewhere, such as:
这不会影响其他地方出现的“+”,例如:
<tag>Bill + Ted+++</tag>
will become
<tag>Bill + Ted</tag>
#3
0
Maybe you can use
也许你可以使用
if (xml.contains("+")) {
xml = xml.replace("+", "");
}
instead of
xml = xml.replaceAll("[+ ]","").trim();
with jdk 1.4 you can use
用jdk 1.4你可以使用
if (xml.indexOf("+") != -1) {
xml = xml.replace("+", "");
}