java - 当节点有多个值时如何使用JAVA替换XML标签中的值

标签 java xml

我需要替换 sEmployee 和 sWUnumber 的值(上面以黄色突出显示)

到目前为止我能做的就是替换节点值和其他属性。但是当在标签中时。我似乎无法替换 sEmployee 和 SWUnumber。我假设这些元素不是属性?

这是我到目前为止所做的事情。

    DocumentBuilderFactory docFactory = DocumentBuilderFactory
            .newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(Constant.Path_OldXmlFile);

    // Get Employee ID, I'm getting my values in excel data so don't mind this
    String sNewEmployeeID = ExcelUtils.getCellData(iTestCaseRow,
            Constant.Personnel_NewEmployeeID);

    // Get Work Unit Number, I'm getting my values in excel data so don't mind this
    String sWorkUnitNumber = ExcelUtils.getCellData(iTestCaseRow,
            Constant.Personnel_WorkUnit);

最佳答案

例如,您可以使用 xPath 查询文档中的节点并替换其文本内容

try {
    // Load the document
    DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
    DocumentBuilder b = f.newDocumentBuilder();
    Document original = b.parse(...);

    // Surgically locate the node you're after
    String expression = "/SyncPersonnel/ApplicationArea/BODID";
    XPath xPath = XPathFactory.newInstance().newXPath();
    Node node = (Node) xPath.compile(expression).evaluate(original, XPathConstants.NODE);
    // Get the nodes current text content
    String value = node.getTextContent();
    System.out.println(value);

    // Replace the values
    value = value.replace("sEmployee", "BananaMan").replace("sWUnumber", "007");
    // Set the text content with the new value
    node.setTextContent(value);

    // Save the new document
    try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {

        Transformer tf = TransformerFactory.newInstance().newTransformer();
        tf.setOutputProperty(OutputKeys.INDENT, "yes");
        tf.setOutputProperty(OutputKeys.METHOD, "xml");
        tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

        DOMSource domSource = new DOMSource(original);
        StreamResult sr = new StreamResult(os);
        tf.transform(domSource, sr);

        String text = new String(os.toByteArray());
        System.out.println(text);

    } catch (TransformerException ex) {
        ex.printStackTrace();
    }

} catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException | DOMException exp) {
    exp.printStackTrace();
}

使用...

<?xml version="1.0" encoding="UTF-8"?>
<SyncPersonnel>
  <ApplicationArea>
    <BODID>...-nid:LSAPPS:3004::sEmployee:0?Personnel&amp;verb=Sync&amp;workunit=sWUnumber</BODID>
  </ApplicationArea>  
</SyncPersonnel>

上面的代码将产生

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SyncPersonnel>
  <ApplicationArea>
    <BODID>...-nid:LSAPPS:3004::BananaMan:0?Personnel&amp;verb=Sync&amp;workunit=007</BODID>
  </ApplicationArea>  
</SyncPersonnel>

关于java - 当节点有多个值时如何使用JAVA替换XML标签中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33227399/

相关文章:

java - 如何关闭 Java Stream?

java - 打印与时间相关的字符串,如 "today"和 "yesterday"根据区域设置翻译?

java - Android - JNI 问题

java - 如何使用 SAX 解析器在 XML 中添加元素?

xml - XSLT 从特定组的节点 'above' 读取属性?

java - Spring MVC 转换服务默认值

java - 关于设置对象属性的机制

c++ - 使用 boost 从二进制到 xml 序列化

javascript - 悬停时用 XML 内容更新 DIV

.net - 以编程方式检索 xml 文档注释