报错信息:
org.xhtmlrenderer.util.XRRuntimeException: Can't load the XML resource (using TrAX transformer). org.xml.sax.SAXParseException; lineNumber: 343; columnNumber: 50; The content of elements must consist of well-formed character data or markup.
at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.transform(XMLResource.java:222)
at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource.java:181)
at org.xhtmlrenderer.resource.XMLResource.load(XMLResource.java:84)
at org.xhtmlrenderer.pdf.ITextRenderer.setDocumentFromString(ITextRenderer.java:168)
at org.xhtmlrenderer.pdf.ITextRenderer.setDocumentFromString(ITextRenderer.java:163)
at com.service.common.util.FileUtil.strToPdfFile(FileUtil.java:62)
at com.service.signContract.MerchantPayContractService.signContract(MerchantPayContractService.java:110)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: javax.xml.transform.TransformerException: org.xml.sax.SAXParseException; lineNumber: 343; columnNumber: 50; The content of elements must consist of well-formed character data or markup.
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:755)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:357)
at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.transform(XMLResource.java:220)
... 13 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 343; columnNumber: 50; The content of elements must consist of well-formed character data or markup.
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:674)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:743)
... 15 more
原因是因为XML字符串内容“...高新区江南路XX号<14-27>”里面包含了额外的“<“、”>”号,需要转义
类似报错
org.xhtmlrenderer.util.XRRuntimeException: Can't load the XML resource (using TrAX transformer). org.xml.sax.SAXParseException; lineNumber: 337; columnNumber: 47; The reference to entity "bar" must end with the ';' delimiter.
at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.transform(XMLResource.java:222)
at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource.java:181)
at org.xhtmlrenderer.resource.XMLResource.load(XMLResource.java:84)
at org.xhtmlrenderer.pdf.ITextRenderer.setDocumentFromString(ITextRenderer.java:168)
at org.xhtmlrenderer.pdf.ITextRenderer.setDocumentFromString(ITextRenderer.java:163)
at com.common.util.FileUtil.strToPdfFile(FileUtil.java:62)
at com.service.signContract.MerchantPayContractService.signContract(MerchantPayContractService.java:110)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: javax.xml.transform.TransformerException: org.xml.sax.SAXParseException; lineNumber: 337; columnNumber: 47; The reference to entity "bar" must end with the ';' delimiter.
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:755)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:357)
at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.transform(XMLResource.java:220)
... 13 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 337; columnNumber: 47; The reference to entity "bar" must end with the ';' delimiter.
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:674)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:743)
... 15 more
原因是因为XML字符串内容"....店名 三十cafe&bar"里面包含了额外的“&”号,也需要转义
转义对照表:
https://bbs.huaweicloud.com/blogs/312362
编码实现:
/**
* 将字符串中的XML特殊字符转义
* & -> &
* < -> <
* > -> >
* " -> "
* ' -> '
* **/
public static String covertXML(String content) {
char[] originContentChars = content.toCharArray();
int finalContentCharLength = 0;//转义后字符串长度
for (char contentChar : originContentChars) {
switch (contentChar) {
case '&':
finalContentCharLength = finalContentCharLength + 5;
continue;
case '"':
case '\'':
finalContentCharLength = finalContentCharLength + 6;
continue;
case '<':
case '>':
finalContentCharLength = finalContentCharLength + 4;
continue;
default:
finalContentCharLength = finalContentCharLength + 1;
}
}
char[] newContentChars = new char[finalContentCharLength];
int newContentCharsPos = 0;
for (char originContentChar : originContentChars) {
switch (originContentChar) {
case '&':
//& -> &
newContentChars[newContentCharsPos] = '&';
newContentChars[newContentCharsPos + 1] = 'a';
newContentChars[newContentCharsPos + 2] = 'm';
newContentChars[newContentCharsPos + 3] = 'p';
newContentChars[newContentCharsPos + 4] = ';';
newContentCharsPos += 5;
continue;
case '"':
//" -> "
newContentChars[newContentCharsPos] = '&';
newContentChars[newContentCharsPos + 1] = 'q';
newContentChars[newContentCharsPos + 2] = 'u';
newContentChars[newContentCharsPos + 3] = 'o';
newContentChars[newContentCharsPos + 4] = 't';
newContentChars[newContentCharsPos + 5] = ';';
newContentCharsPos += 6;
continue;
case '\'':
//' -> '
newContentChars[newContentCharsPos] = '&';
newContentChars[newContentCharsPos + 1] = 'a';
newContentChars[newContentCharsPos + 2] = 'p';
newContentChars[newContentCharsPos + 3] = 'o';
newContentChars[newContentCharsPos + 4] = 's';
newContentChars[newContentCharsPos + 5] = ';';
newContentCharsPos += 6;
continue;
case '<':
//< -> <
newContentChars[newContentCharsPos] = '&';
newContentChars[newContentCharsPos + 1] = 'l';
newContentChars[newContentCharsPos + 2] = 't';
newContentChars[newContentCharsPos + 3] = ';';
newContentCharsPos += 4;
continue;
case '>':
//> -> >
newContentChars[newContentCharsPos] = '&';
newContentChars[newContentCharsPos + 1] = 'g';
newContentChars[newContentCharsPos + 2] = 't';
newContentChars[newContentCharsPos + 3] = ';';
newContentCharsPos += 4;
continue;
default:
newContentChars[newContentCharsPos] = originContentChar;
newContentCharsPos++;
}
}
return new String(newContentChars);
}