【问题标题】:How can I parse android activity_main.xml file and modify it?如何解析 android activity_main.xml 文件并修改它?
【发布时间】:2023-04-02 11:40:01
【问题描述】:

我想解析 android activity_main.xml 并务实地在其中添加我的视图的自定义属性标签。

例如:(原创)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    <view
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        class="com.test.compoundviewcreation.Slider"
        android:id="@+id/view4"/>
</RelativeLayout>

修改后:(添加自定义:sliderLabel标签)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    <view
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        class="com.test.compoundviewcreation.Slider"
        android:id="@+id/view4"
        custom:sliderLabel="Custom SLider Label"/>
</RelativeLayout>

我尝试使用 DOM Parser,但它无法解析它并显示 xml doc 为空。有什么想法吗?

【问题讨论】:

  • 为什么以编程方式?有什么要求?为每个&lt;view&gt; 添加相同的属性?有了提供的信息,我会手动完成。
  • @Birei 因为不想手动修改。 Android Studio 布局设计器属性面板仅显示 android 属性,不显示自定义属性。我将通过一个简单的 java 应用程序创建另一个属性面板,它允许输入自定义属性。所以,我需要解析这个 XML 文件。是的,我想为不同的自定义视图添加相同的属性。
  • 我的主管不想手动操作,我想为他提供一种解决方案。

标签: java android xml parsing


【解决方案1】:

您可以尝试使用joox 库,使用 导入,例如:

compile 'org.jooq:joox:1.3.0'

那么您可以使用find() 方法来获取所有&lt;view&gt; 元素和attr() 方法来分配您的自定义属性,例如:

import java.io.File;
import java.io.IOException;

import org.joox.Match;
import org.xml.sax.SAXException;
import static org.joox.JOOX.$;

public class Main {

    public static void main(String[] args) throws SAXException, IOException {
        final Match $m = $(new File(args[0]));
        $m.find("view").attr("custom:sliderLabel", "Custom SLider Label");
        $m.write(System.out);
    }
}

我会假设您的 xml 格式正确(不像您的示例)并且您将 xml 文件作为参数提供给程序。我还添加了另一个 &lt;view&gt; 以表明它改变了它们。

请注意,输出既不是很好的格式也不是有序的(属性),但这不是xml 问题。结果:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_height="match_parent" android:layout_width="match_parent">

    <view android:id="@+id/view4" android:layout_height="wrap_content" android:layout_width="match_parent" class="com.test.compoundviewcreation.Slider" custom:sliderLabel="Custom SLider Label"/>

    <view android:id="@+id/view5" android:layout_height="wrap_content" android:layout_width="match_parent" class="com.test.compoundviewcreation.Slider" custom:sliderLabel="Custom SLider Label"/>

</RelativeLayout>

更新:基于 cmets,假设您只想将新属性附加到 idview4&lt;view&gt;,您需要使用命名空间。我更喜欢使用xpath() 方法来处理这些情况,例如:

public static void main(String[] args) throws IOException, SAXException {
    final Match $m = $(new File(args[0]));
    $m.namespace("android", "http://schemas.android.com/apk/res/android")
            .xpath("//view[@android:id='@+id/view4']")
            .attr("custom:sliderLabel", "Custom SLider Label");
    $m.write(System.out);
}

【讨论】:

  • 谢谢,我正在尝试。
  • 非常感谢,它的工作。如果您能帮助我了解如何使用过滤器以及如何编写 xml,我将非常高兴。以下 2 个命令不起作用: $m.find("view").filter("custom:sliderLabel").after("");和 $m.write("C:\\Users\\Cubex\\Desktop\\customer1.xml"); .我没有找到任何有关 joox 的文档,也不明白我的错误在哪里。非常感谢您提供的出色解决方案。
  • @Newbie23:对于第二个,您需要一个File 对象,因此将一个实例化为write() 方法的参数,例如:$m.write(new File("your/path/to/file"))。对于第一个,你想达到什么目的?
  • 谢谢!!我想为不同的 id 附加不同的新属性。所以,到目前为止,我已经成功找到了 id 列表。如果过滤器功能有效,那也可以达到目的。现在,如果我只想为视图 4 更改 custom:sliderLabel,而不是为 view5(假设有两个视图),我该怎么做?
  • @Newbie23:我已经更新了答案以包含这个事实。
猜你喜欢
  • 2016-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-23
相关资源
最近更新 更多