Don't want to change ' entity to ' in XML file
I am automating changing of some XML files through documentation builder factory
but in the one of the attribute entity reference '
getting change to '
. I don't want that change to happen.
docBuilderFactory.setValidating(false);
docBuilderFactory.setExpandEntityReferences(false);
but nothing seems to work
This is a more of a hack, but you can escape it before processing:
myXmlString = myXmlString.replaceAll("'", "'").
Then the processor will expand '
to '
.
The real issue is that '
is merely a way to escape the apostrophe. This is like reading a string in JavaScript: '\''
, once the data is interpreted as string the escaped characters (entities in XML) are expanded by the parser. That is, the parsing step from raw characters to = fundamentally includes this interpretation and there is no good way around it.
Presumably the real problem is output of valid XML after transformation, where the issue lies in the fact that your original input string is parsed and the entity expanded to apostrophe. Fortunately this is an easy fix: if you know certain attributes might have apostrophes, you can code your output step to include a search & replace pass, where '
is converted into '
.
(This is similar to writing out raw JavaScript strings in JavaScript and having to convert apostrophes back to \'
)
精彩评论