Android placement of custom XML files - android

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.

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

How to get embbed XML from XML

I have a little problem, during sending request to serwer I'm getting xml with embedded xml in it. Application works on Android 2.1 so I can not just use getTextContent() to get value, so I'm using Node.getFirstChild().getNodeValue(); and for standard node with text it works fine but for node with embedded xml in it, it isn't. Does someone have any advice or had the same problem? How to solve it.
BTW: When I checked type of node all of the are 1 so it means ELEMENT_NODE.
What exactly are you receiving from server? Something like;
<?xml version="1.0"?>
<embedded-xml>
<?xml version="1.0"?>
<data/>
</embedded-xml>
Or;
<?xml version="1.0"?>
<embedded-xml>
<data/>
</embedded-xml>
In latter case you can simply retrieve data -node (not value), there's no need to parse it separately, while first case isn't exactly valid afaik.
You can't embed an arbitrary XML document in an XML document because XML documents can include sequences which are illegal with CDATA sections that are required to embed XML in the first place.
If the task is being able to embed, then extract, an arbitrary XML document, you have to encode the XML document, then embed it in a CDATA section. The reason is that the closing tag for CDATA is "]]>" and if the (arbitrary) embedded document contains a CDATA section, the "]]>" tag for that documents CDATA section will close the CDATA section in the parent.
In my experience, the best approach to embedding XML inside of XML is to compress (because Base64 encoding is going to blow it up ...) then Base64 encode. The reason I've taken this approach is because it is fool-proof and the two transforms (some kind of compressor and base64 en/de-coding) are widely available.

Categories

Resources