Reputation: 251
I want to parse android activity_main.xml and add custom properties tag of my views in it pragmatically.
For example: (original)
<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>
After modification: (custom:sliderLabel tag added)
<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>
I tried with DOM Parser but it cant parse it and shows xml doc is null. Any idea?
Upvotes: 0
Views: 199
Reputation: 36282
You can try with joox
library, imported using gradle like:
compile 'org.jooq:joox:1.3.0'
So then you can use find()
method to get all <view>
elements and attr()
method to assign your custom attribute, like:
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);
}
}
I will assume your xml
is well-formed (not like your example) and that you give the xml
file as argument to the program. Also I added another <view>
to show that it changes both of them.
Note that output is neither well formatted nor ordered (attributes), but that is not a xml
problem. Result:
<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>
UPDATE: Based in comments, assuming you want to only append the new attribute to the <view>
with id
of view4
, you need to use namespaces. I prefer to hanlde these cases with xpath()
method, like:
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);
}
Upvotes: 1