Access from xml to other xml? - android

How to access from xml file to values in other xml. I need to access to xml which was created by SharedPreferences and is in folder shared_prefs.

You can do an "inner" join with XSLT

Related

How to pass XML file to layout custom attribute?

While developing a library module I decided to extern all UI attributes in a metadata file using XML - which will be latter validated againts a XSD schema. Currently I have both, XML and XSD, on assets folder of my project. I want to pass the XML reference in a custom attribute for my Custom View - doing something like app:cestyle="#assets/my_xml_file". Is that possible?
Assets are not mapped in R file so you can't access them like #string or #dimen, etc.
I would suggest either using a string resource that equals asset file name or res/raw folder to hold your xml files.

Using strings.xml in raw xml resource

I was wondering if it's possible to load resources from strings.xml in raw xml resource. Example:
<root>
<element>#string/res1</element>
<element>#string/res2</element>
<element>#string/res3</element>
</root>
Is this possible?
If it's not possible, if I create a raw xml resource, do I have to provide multiple files for localization purposes?
Are you trying to use values declared in strings.xml in a file inside /raw/? Then no, that's not possible. If you want to internationalize your "raw" XMLs you can provide one for each of your supported languages in the appropriate folder (/raw-fr/, /raw-en/, etc). At least that's out of the box.
Alternatively, you can establish a template format and parse the raw XMLs in Java and fetch the appropriate string resource from there instead.

Is it possible to convert java views to xml layout?

I a created a linearlyaout and added views to it using java code. is it possible to convert this layout to xml layout and save it to the storage ?
There is nothing stopping you from parsing all those Views attributes and then building a xml layout file. However you will not be able to use that constructed xml layout(like setContentView(R.layout.built_layout)) like other layouts from the res/layout folder.

Is it possible reuse android resource file parser to read custom resource file?

Imagine the following scenario: some xml file is downloaded from server and stored on device. Inside this file is the proper android resource xml markup, for example:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="string_array_name">
<item>text_string</item>
</string-array>
</resources>
I wonder is it possible to somehow create a custom Resources object and make it read this custom xml file - so I can just reuse existing resource xml parsing mechanism?
Or is this a no-go and I must use XmlPullParser in this case?
You can't reuse resource parser because it parses binary xml files, not text xml files.
You probably could've converted your xml into Android binary format and then somehow trick resource parser (assuming this is possible). But converting text xml to binray xml on server-side is relatively harder task then just write your custom XML parser.
By the way, the aapt tool is responsible for text to binary xml conversion (as part of android build process).

Android placement of custom XML files

I have a large XML file which is arranged like so:
<item><title>...</title><link>...</link></item>
How can I use parse(new InputSource()); to point to this XML file if it's stored within my project directory and where do I put the XML file?
The best answer ignores your parse(new InputSource()) request. Put it in res/xml/ and use getResources().getXml(). This gives you an XmlPullParser on your data. The big advantage here is that your XML is somewhat pre-parsed during the compile step, and so parsing is about ten times faster at runtime than with normal XML parsers.
If it absolutely positively has to use DOM or SAX, put it in res/raw/, then use getResources().openRawResource() to get an InputStream, which you can wrap in an InputSource.

Categories

Resources