2012-03-27 119 views
2

我想更新從另一個XML file.I XML文件已使用的XML文件,如下所示:如何動態更新另一個xml文件的xml文件?

one.xml

<?xml version="1.0" encoding="utf-8"?> 
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:background="#00BFFF"> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:gravity="center" android:visibility="visible"> 
</LinearLayout> 
<LinearLayout 
    android:id="@+id/linearLayout2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:gravity="center" android:visibility="visible"> 

</LinearLayout> 

</LinearLayout> 
</ScrollView> 

two.xml如下:

 <?xml version='1.0' encoding='utf-8' standalone='yes' ?> 
     <map> 
     <int name="linearLayout1" value="8" /> 
     <int name="linearLayout2" value="0" /> 
     </map> 

從上面的兩個xml文件我想更改屬性值之一。當 如果

<int name ="linearLayout1" value = "8"/> 

two.xml XML那麼我想更新one.xml文件作爲其中的LinearLayout 機器人:ID = 「@ + ID/linearLayout1」然後更改屬性值as android:visibility =「gone」

+0

「改變屬性值之一。 xml when if ** from ** two.xml「,解釋一下更多。 – 2012-03-27 06:04:10

+0

編輯內容 – 2012-03-27 06:08:57

+0

我想獲得更新one.xml文件 – 2012-03-27 06:09:42

回答

1

這裏是代碼,你想那是什麼

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); 
    Document doc = docBuilder.parse("/home/riddhish/developerworkspace/SplitString/src/com/updatexmlwithjava/two.xml");   
    DocumentTraversal traversal = (DocumentTraversal) doc; 
    Node a = doc.getDocumentElement(); 
    NodeIterator iterator = traversal.createNodeIterator(a, NodeFilter.SHOW_ELEMENT, null, true); 

/** * 邏輯檢查 **/

boolean flag=false; 
for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) { 
     Element e = (Element) n;     
      if ("int".equals(e.getTagName())) { 
       if(e.getAttribute("name").equals("linearLayout1")){ 
         if(e.getAttribute("value").equals("8")) 
          flag=true;      
        }      
      } 
} 

/** * 邏輯閱讀one.xml和設置android:visibility =「gone」 **/

docFactory = DocumentBuilderFactory.newInstance(); 
docBuilder = docFactory.newDocumentBuilder(); 
doc = docBuilder.parse("/home/riddhish/developerworkspace/SplitString/src/com/updatexmlwithjava/one.xml");   
traversal = (DocumentTraversal) doc; 
a = doc.getDocumentElement(); 
iterator = traversal.createNodeIterator(a, NodeFilter.SHOW_ELEMENT, null, true);    
for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) { 
     Element e = (Element) n;     
      if ("LinearLayout".equals(e.getTagName())) { 
       if(e.getAttribute("android:id").equals("@+id/linearLayout1")){ 
         if(flag==true){ 
          System.out.println(""+e.getAttribute("android:visibility")); 
          e.setAttribute("android:visibility", "gone"); 
         }       
       }      
      } 
} 

/** *邏輯重寫one.xml **/

TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
Transformer transformer = transformerFactory.newTransformer(); 
DOMSource source = new DOMSource(doc); 
StreamResult result = new StreamResult(new File("/home/riddhish/developerworkspace/SplitString/src/com/updatexmlwithjava/one.xml")); 
iterator = traversal.createNodeIterator(a, NodeFilter.SHOW_ELEMENT, null, true);   
doc = docBuilder.newDocument(); 
Element rootElement = doc.createElement("ScrollView"); 
doc.appendChild(rootElement); 
for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {    
     rootElement.appendChild(doc.importNode(n, true)); 
} 
transformer.transform(source, result); 
+0

感謝你的答案,但它僅適用於linearLayout1。如果有人修改two.xml那麼我們應該如何動態編寫。 – 2012-03-27 07:16:35

+0

你有getAttribute(),getTagName() ,getTextContent()動態檢查屬性的值,所以它取決於你的邏輯如何設置。 – 2012-03-27 07:21:53