gpt4 book ai didi

java - 如何从另一个 xml 文件动态更新 xml 文件?

转载 作者:行者123 更新时间:2023-11-30 11:46:15 25 4
gpt4 key购买 nike

我想从另一个 xml 文件更新一个 xml 文件。我使用了一个 xml 文件,如下所示:

一个.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 文件中更改属性值 1。 xml什么时候如果

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

two.xml 然后我想更新 one.xml 文件作为 LinearLayout android:id="@+id/linearLayout1" 然后将属性值更改为 android:visibility="gone"

最佳答案

这是你想要的代码

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

关于java - 如何从另一个 xml 文件动态更新 xml 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9884051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com