2015-10-20 56 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