将包含SOAP消息的XML文件转换为字符串并更新特定标记的值

时间:2017-02-09 15:17:12

标签: 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>

我最初做的是用新用户更新xml文件本身并传递,如下面的代码所示。

try {
        String namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
        configProperties.load(SecurityTokenHandler.class.getResourceAsStream(PROPERTIES_FILE));
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        Document requestDoc = documentBuilderFactory.newDocumentBuilder().parse(SecurityTokenHandler.class.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();
        DOMSource source = new DOMSource(requestDoc);
        StreamResult result = new StreamResult(SecurityTokenHandler.class.getResource(SOAP_REQUEST_FILE).getFile());
        docTransformer.transform(source, result);
    } catch(IOException | ParserConfigurationException | SAXException | TransformerException exception) {
        LOGGER.error("There was an error loading the properties file", exception);
    }

但是,我后来发现,因为这是一个资源文件,我不允许修改文件本身。我必须将xml文件存储为字符串,更新用户和密码值而不修改文件,然后返回带有更新值的xml文件的字节数组(不修改原始文档)。知道我怎么能做到这一点吗?

1 个答案:

答案 0 :(得分:0)

所以我提出的解决方案是基本上将结果更改为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);
    }