2015-10-20 70 views
0

我需要更换sEmployee和sWUnumber值多值(突出表现在上面黄色)如何在XML标记替换值时,节点在使用JAVA

到目前为止,我所能做的就是替换节点值和其他属性。但是,当在标签。我似乎无法取代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); 
+1

获取'Node''BODID',得到它的价值,使用一个简单的'String#replace',然后将新值重新设置为'Node'并保存 – MadProgrammer

回答

1

您可以使用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> 
+0

@ user3713453这是一个相对基本的/减少版本,但应该给你一些东西从 – MadProgrammer

+0

工作像一个魅力!谢谢你。标记为已回答。 – user3713453