我有一个java类XMLHelper.java,它有一个名为parseFromString的方法。该方法接收字符串 xmlsoap 请求,解析它并将其转换为文档。此方法将十六进制字符转换为 UTF-8。我不知道这是否正确...
在照片示例中,在 dBuilder.parse(is) 之后,字符 & #xED;转换为“í”。
public class XMLHelper {
public static DocumentBuilderFactory dbFactory = null;
static {
dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setNamespaceAware(true);
dbFactory.setIgnoringElementContentWhitespace(true);
}
public static SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
public static Document parseFromString (String xmlInString) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilder dBuilder = XMLHelper.dbFactory.newDocumentBuilder();
InputSource is = new InputSource (new StringReader(xmlInString));
Document doc = dBuilder.parse(is);
return doc;
}
....
}
然后,代码继续如下:
soapEnvelopeRequest = XMLHelper.parseFromString(toString(soapEnvelopeRequest));
wsSecHeader.insertSecurityHeader(soapEnvelopeRequest);
wsSecSignature.prepare(soapEnvelopeRequest, crypto, wsSecHeader);
// Modificacion y firma de la peticion
secSOAPReqDoc = wsSecSignature.build(soapEnvelopeRequest, crypto,
wsSecHeader);
element = secSOAPReqDoc.getDocumentElement();
// Transformacion del elemento DOM a String
source = new DOMSource(element);
baos = new ByteArrayOutputStream();
streamResult = new StreamResult(baos);
TransformerFactory.newInstance().newTransformer()
.transform(source, streamResult);
secSOAPReq = new String(baos.toByteArray());
// Creacion de un nuevo mensaje SOAP a partir del mensaje SOAP
// securizado formado
MessageFactory mf = new org.apache.axis.soap.MessageFactoryImpl();
res = mf.createMessage(null,
new ByteArrayInputStream(secSOAPReq.getBytes()));
return res;
变量 baos 将字符转换为:“marín”,我收到此错误:
org.apache.axis.AxisFault: ; nested exception is: java.lang.RuntimeException: Invocation arguments were modified at org.apache.axis.AxisFault.makeFault(AxisFault.java:101)
谢谢。
好吧,这是一个经典的 UTF-8 到 ISO-8859-1 编码错误/问题。 Unicode 字符“LATIN SMALL LETTER I WITH ACUTE”、U+00ED 或 í 在 UTF-8 中编码为
0xC3AD
。如果您在 ISO-8859-1 中解释该字节序列,您会看到 0xC3 或 U+003C、“带波浪号的拉丁大写字母 A”和 0xAD,它是“软连字符”,即仅可见的符号如果涉及换行。
因此,您需要更好地控制 XML 数据的反序列化和序列化链,并确保所有阶段都能正确生成和解释字节序列。
希望这有帮助