Java 将包含SOAP消息的XML文件转换为字符串,并更新特定标记的值
因此,我想看看是否有办法将带有soap消息的XML文件转换为字符串,然后更新特定标记的值。这里是我正在谈论的标签Java 将包含SOAP消息的XML文件转换为字符串,并更新特定标记的值,java,xml,xpath,soap,Java,Xml,Xpath,Soap,因此,我想看看是否有办法将带有soap消息的XML文件转换为字符串,然后更新特定标记的值。这里是我正在谈论的标签 <o:Username>Bill</o:Username> <o:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">Hello123</o:Password>
<o:Username>Bill</o:Username>
<o:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">Hello123</o:Password>
然而,我后来发现,由于这是一个资源文件,我不允许修改文件本身。我必须将xml文件存储为字符串,在不修改文件的情况下更新用户和密码值,然后返回xml文件的字节数组和更新后的值(不修改原始文档)。你知道我怎样才能做到这一点吗?所以我提出的解决方案基本上是将结果更改为byteArrayOuputStream,而不是xml文件本身。发布更新代码:
try {
String namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
configProperties.load(SecurityTokenHandler.class.getClassLoader().getResourceAsStream(PROPERTIES_FILE));
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
Document requestDoc = documentBuilderFactory.newDocumentBuilder().parse(SecurityTokenHandler.class.getClassLoader().getResourceAsStream(SOAP_REQUEST_FILE));
Element docElement = requestDoc.getDocumentElement();
docElement.getElementsByTagNameNS(namespace, "Username").item(0).setTextContent(configProperties.getProperty("username"));
docElement.getElementsByTagNameNS(namespace,"Password").item(0).setTextContent(configProperties.getProperty("password"));
Transformer docTransformer = TransformerFactory.newInstance().newTransformer();
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
StreamResult result = new StreamResult(byteArrayOutputStream);
DOMSource source = new DOMSource(requestDoc);
docTransformer.transform(source, result);
b = byteArrayOutputStream.toByteArray();
}
} catch(IOException | ParserConfigurationException | SAXException | TransformerException exception) {
LOGGER.error("There was an error loading the properties file", exception);
}
@aUserHimself:有什么线索吗?使用XSLT转换如何?看起来这将是一个简单的转换。如果您认为这是一个选项,请将问题重新表述为:显示您期望的完整XML输入文件和输出XML文件。
try {
String namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
configProperties.load(SecurityTokenHandler.class.getClassLoader().getResourceAsStream(PROPERTIES_FILE));
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
Document requestDoc = documentBuilderFactory.newDocumentBuilder().parse(SecurityTokenHandler.class.getClassLoader().getResourceAsStream(SOAP_REQUEST_FILE));
Element docElement = requestDoc.getDocumentElement();
docElement.getElementsByTagNameNS(namespace, "Username").item(0).setTextContent(configProperties.getProperty("username"));
docElement.getElementsByTagNameNS(namespace,"Password").item(0).setTextContent(configProperties.getProperty("password"));
Transformer docTransformer = TransformerFactory.newInstance().newTransformer();
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
StreamResult result = new StreamResult(byteArrayOutputStream);
DOMSource source = new DOMSource(requestDoc);
docTransformer.transform(source, result);
b = byteArrayOutputStream.toByteArray();
}
} catch(IOException | ParserConfigurationException | SAXException | TransformerException exception) {
LOGGER.error("There was an error loading the properties file", exception);
}