问题
I start to work with Xstream
with Java.
I have a list of cars named CarList
.
I have a car as an object named Car
.
XStream xstream = new XStream(new StaxDriver());
xstream.alias("Car", Car.class);
xstream.alias("Cars", CarList.class);
xstream.addImplicitCollection(CarList.class, "list");
xstream.toXML(list, new FileWriter(fileName+xmlExtension));
Unfortunately each time I save my file. My XML contains "
and &
instead of "
and &
.
What am I doing wrong? Should I use a regex for replacing those characters or there is something better to do?
回答1:
I found this problem quite intersting, so I made a little investigation about it.
XStream doesn't write XML on its own, it uses various libs ("drivers"?) to do so. Therefore, the reason could be found in concrete implemenation of using HierarchicalStreamDriver
.
First of all, I have tried to debug with XStream xstream = new XStream()
. In this case you can see a switch
operatior to write these replacements (see PrettyPrintWriter.writeText()
):
private void writeText(String text) {
int length = text.length();
for (int i = 0; i < length; i++) {
char c = text.charAt(i);
switch (c) {
case '\0':
this.writer.write(NULL);
break;
case '&':
this.writer.write(AMP);
break;
case '<':
this.writer.write(LT);
break;
case '>':
this.writer.write(GT);
break;
case '"':
this.writer.write(QUOT);
break;
case '\'':
this.writer.write(APOS);
break;
case '\r':
this.writer.write(SLASH_R);
break;
default:
this.writer.write(c);
}
}
}
Second, I have tried that with your example with XStream xstream = new XStream(driver)
. The final debug point with available sources (I did not go deeper) is StaxWriter.setValue()
. It means, that reason is not in XStream
itself, but in the XMLStreamWriter
that is used to write a xml file. I have found XMLStreamWriter writeCharacters without escaping
. I have tried one given solution: streamWriterFactory.setProperty("escapeCharacters", false);
and my current solution is:
StaxDriver driver = new StaxDriver();
driver.getOutputFactory().setProperty("escapeCharacters", false);
XStream xstream = new XStream(driver);
I have fixed the problem of replacing "
and &
. But problem is that if you have e.g. <>
in one of your strings - they will not be changed as well, and it breaks the final xml.
So, I do not know your context, probably this light solution could be enough for you. If not and you have <>
symbols you your output, then I would find a place where driver reads escapeCharacters property and find out how to disable only specific set of escape characters.
I do not know is there any standard way or not, sorry. But it seems that all results I have found are not universal and to solve this correct, we have to know a task context. One of not bad solution, as you mentioned, is just replace required escape symbols in final xml: XStream apostrophe Issue in converting Java Object to XML and How can I disable unnecessary escaping in XStream? .
来源:https://stackoverflow.com/questions/48141168/how-to-avoid-xstream-to-produce-xml-file-with-amp-or-quote-or-similar-chars