2015-01-08 20 views
0

我有下面的XML文件我想改變一些屬性是xml文件使用Java代碼。 我們通過java代碼閱讀和XML文件更新多層次的元素在Java中

這裏改變了一些屬性下面XML文件包含相關信息。

<Order> 
    <AllowedModifications> 
    <Modification ModificationType="CHANGE_CUSTOM_ATTRIBUTES" ThroughOverride="Y"/> 
    <Modification ModificationType="RECEIVING_NODE" ThroughOverride="Y"/> 
    <Modification ModificationType="OTHERS" ThroughOverride="Y"/> 
    </AllowedModifications> 
    </Order> 

就像ModificationType = OTHERS那麼我們必須改變ThroughOverride值= Y。我們如何在java代碼的幫助下做到這一點。

我想下面的代碼,但它沒有改變。

Element eleAllowedModifications = Util.getChildElement(eleOrderRoot,"AllowedModifications") 
    System.out.println("First Element "+eleAllowedModifications.getNodeName()); 
    Node staff = xmlFile.getElementsByTagName("Modification").item(0); 
    NamedNodeMap attr = staff.getAttributes(); 
     for (int i = 0; i < attr.getLength(); i++) { 
    Node nodeAttr = attr.getNamedItem("ModificationType"); 
    MoficationTYp =String.valueOf(nodeAttr); 
    Node nodeAttr1= attr.getNamedItem("ThroughOverride"); 
    MoficationTp =String.valueOf(nodeAttr); 
    } 

    if (MoficationTYp=="OTHERS") { 
    for (int i = 0; i < attr.getLength(); i++) { 
    Node nodeAttr = attr.getNamedItem("ThroughOverride"); 
    MoficationTYp =String.valueOf(nodeAttr); 
    nodeAttr.setNodeValue("Y");     
    } 

    } 

這裏它沒有改變ThroughOverride屬性的值。

+0

有人能告訴我我們該怎麼辦 –

+1

嗯,我想知道是否使用MoficationTYp ==「OTHERS」是個好主意(至少可以說)。比較字符串應該用'.equals()'方法完成。 – potame

+0

它也沒有工作我已嘗試 –

回答

0

如果使用XML library(披露:我是附屬於該項目),您可以用更少的代碼讓你的結果:

public class UpdateOrder { 

    public interface Order { 
     @XBUpdate("//Modification[@ModificationType='OTHERS']/@ThroughOverride") 
     void updateOrder(String value); 
    } 
    public static void main(String[] args) throws IOException {  
     Order order = new XBProjector(Flags.TO_STRING_RENDERS_XML).io().url("res://data.xml").read(Order.class); 
     order.updateOrder("N"); 
     System.out.println(order.toString()); 
    } 

} 

這個程序打印出:

<Order> 
    <AllowedModifications> 
     <Modification ModificationType="CHANGE_CUSTOM_ATTRIBUTES" ThroughOverride="Y"/> 
     <Modification ModificationType="RECEIVING_NODE" ThroughOverride="Y"/> 
     <Modification ModificationType="OTHERS" ThroughOverride="N"/> 
    </AllowedModifications> 
</Order> 
+0

不打印輸出它給java.net.MalformedURLException –

+0

@Cfx XMLBean的似乎是一個有趣的圖書館。我會很快看看,謝謝你的信息。 – potame

+0

URL協議'res://'用於加載資源。因此,xml文件必須位於您的類路徑中。您可以使用有效的文件url或直接使用文件api:'projector.io.file(「path」)。read(...)'。 – Cfx

0

那麼,我會評價使用以下代碼(例如):

Element eleAllowedModifications = Util.getChildElement(eleOrderRoot,"AllowedModifications"); 
    System.out.println("First Element "+ eleAllowedModifications.getNodeName()); 
    Element staff = (Element)xmlFile.getElementsByTagName("Modification").item(0); 
    moficationTyp = staff.getAttribute("ModificationType"); 
    //moficationTp = String.valueOf(nodeAttr); 
    if (moficationTyp.equals("OTHERS")) { 
     staff.setAttribute("ThroughOverride", "Y"); 
    } 

其他評論:

  • 變量名稱(例如MoficationTYp)應該以小寫字母開頭。
  • 環路在這裏沒有用
  • String.valueOf()在我看來並不合適。