如何更新从另一个XML文件动态xml文件?文件、动态、XML、xml

2023-09-12 00:56:08 作者:﹏1s、耂夫尐萋。

我想更新从另一个XML file.I一个XML文件中使用了一个XML文件,如波纹管:

I would like to update an xml file from another xml file.I have used an xml file as shown bellow:

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文件,我想改变的属性值之一。 XML时 如果

from the above two xml files i would like to change the attribute value one. xml when if

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

从 two.xml ,然后我想更新的 one.xml 文件,其中的LinearLayout 机器人:ID =@ + ID / linearLayout1,然后更改属性值,机器人:知名度=水涨船高。 请任何机构给出programetic想法就可以了。

from two.xml then i would like to update one.xml file as where LinearLayout android:id="@+id/linearLayout1" then change the attribute value as android:visibility="gone". please any body give an programetic idea on it.

推荐答案

下面是code你想要的是

Here is code what you want it's

    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);

/ **     *逻辑检查     ** /

/** * Logic for checking **/

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:知名度=水涨船高 ** /

/** * Logic for reading one.xml and setting 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 ** /

/** * Logic for rewriting 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);
 
精彩推荐
图片推荐