What's the best way to read xml in android? - java

Hi I've an android app which uses an XML file to render its User Interface,And here is the xml
<?xml version="1.0" encoding="utf-8"?>
<questions>
<question id="1" label="Name" type="text"></question>
<question id="2" label="Gender" type="select" options="Male,Female,Transgender"></question>
<question id="3" label="Age" type="text"></question>
<question id="4" label="city" type="select" options="Banglore,Hyderabad,Chennai"></question>
</questions>
I am using DOM parser to read xml and I am getting the inputstream using url.openconnection().getInputstream() method.But I want the xml file to be included in my android project folder and use it.How would I go about that ?? so what are the other ways to get the InputStream ??

If you place the file in your assets folder, you can use the AssetManager to get an input stream to it.

Related

Selective XML parsing

This the xml file I have
<?xml version="1.0" encoding="UTF-8"?>
<Bank>
<Account type="saving">
<Id>1001</Id>
<Name>Jack Robinson</Name>
<Amt>10000</Amt>
</Account>
<Account type="current">
<Id>1002</Id>
<Name>Sony Corporation</Name>
<Amt>1000000</Amt>
</Account>
</Bank>
I need to parse this xml and get the contents between <Bank>...</Bank>. My output xml should be
<Account type="saving">
<Id>1001</Id>
<Name>Jack Robinson</Name>
<Amt>10000</Amt>
</Account>
<Account type="current">
<Id>1002</Id>
<Name>Sony Corporation</Name>
<Amt>1000000</Amt>
</Account>
Any ideas on how to achieve this using Java?
First of all:
your output XML is not valid XML.
XML must have root element which you try to remove.
As #Seelenvirtuose said, there are tons of ways to do what you want on many levels.
From simple manipulating original XML as String and up to using DOM model, JAXB, XPath/XQuery, or XSLT. It is matter of your choice.
As example with Apache commons utils:
String resultString = org.apache.commons.lang.StringUtils.substringBetween(originalXMLString,"<Bank>","</Bank>").trim();
Of course your output can be only String, because it is not valid XML. Then you can do with that String whatever you want - print it, store in file or DB etc...

Java Sax parse Unique XML tag File

I've read a lot about XML and SAX lately but I'm not sure nor have I seen this in any examples or seen it talked about. I have an XML file and I want to get the information from it.
<?xml version='1.0' encoding='UTF-8'?>
<eveapi version="2">
<currentTime>2015-04-30 04:22:22</currentTime>
<result>
<rowset name="characters" key="characterID"columns="name,characterID,corporationName,corporationID,allianceID,allianceName,factionID,factionName">
<row name="Grasume Crendraven" characterID="92916469" corporationName="Hyperion Guard" corporationID="98372642" allianceID="99005157" allianceName="Winmatar Republic" factionID="0" factionName="" />
<row name="Susan Snow" characterID="95325415" corporationName="Federal Navy Academy" corporationID="1000168" allianceID="0" allianceName="" factionID="0" factionName="" />
<row name="Grasume" characterID="95528725" corporationName="School of Applied Knowledge" corporationID="1000044" allianceID="0" allianceName="" factionID="0" factionName="" />
</rowset>
</result>
<cachedUntil>2015-04-30 05:02:33</cachedUntil>
</eveapi>
What I'm trying to do is get the information in the row name section then pull the info I need from that string. I assume the best thing for me to do is to pull the XML section I need and then set up a text parser to get the specific information that I need to be used in the rest of my program.

Include tag in jaxb

I have an xml file of following structure:
<root>
<paramsToInclude>
<params id="id1">
<param11>val1</param11>
<param12>val2</param12>
<param13>val3</param13>
<param14>val4</param14>
</params>
<params id="id3">
<param31>val1</param31>
<param32>val2</param32>
</params>
</paramsToInclude>
<process>
<subprocess1>
<include params="id1"/>
<query>
SELECT *
FROM
table;
</query>
</subprocess2>
<subprocess1>
<rule>rule1</rule>
<rule>rule2</rule>
</subprocess2>
<subprocess3>
<processParam>val1</processParam>
<include params="id2"/>
<include params="id3"/>
</subprocess3>
</process>
I'm using jaxb to parse this xml into the java classes. Is there a way to substitute includes in the process by it's values from the begin of file? I mean, I wan't file to be parsed as if it look's like
<root>
<paramsToInclude>
<params id="id1">
<param11>val1</param11>
<param12>val2</param12>
<param13>val3</param13>
<param14>val4</param14>
</params>
<params id="id3">
<param31>val1</param31>
<param32>val2</param32>
</params>
</paramsToInclude>
<process>
<subprocess1>
<param11>val1</param11>
<param12>val2</param12>
<param13>val3</param13>
<param14>val4</param14>
<query>
SELECT *
FROM
table;
</query>
</subprocess2>
<subprocess1>
<rule>rule1</rule>
<rule>rule2</rule>
</subprocess2>
<subprocess3>
<processParam>val1</processParam>
<param11>val1</param11>
<param12>val2</param12>
<param13>val3</param13>
<param14>val4</param14>
<param31>val1</param31>
<param32>val2</param32>
</subprocess3>
</process>
is it possible t do this? I've found link http://thetechietutorials.blogspot.com/2011/08/jaxb-tutorial-part-2-jaxb-with-xinclude.html how to this include from another file, but comment says that it's impossible to do this for the same file (I understand that I can put this includes in another xml, but I don't think it's a best way). Also I don't want to use hashMap because in this way included params will be stored in hashMap and processParam (from subprocess3) will be class variable.
Is there a way to do this somehow?

Best way to bind a XML File in Java (NetBeans)

i want to bind this simple XML File in my java project:
<?xml version="1.0" encoding="UTF-8"?>
<itg>
<reader>
<chapter id="1">
<subchapter id="1"></subchapter>
<subchapter id="2"></subchapter>
</chapter>
<chapter id="2">
<subchapter id="1"></subchapter>
<subchapter id="2"></subchapter>
</chapter>
<chapter id="3"></chapter>
</reader>
<questions>
</questions>
</itg>
I use NetBeans, and actually i bind the XML File by parsing the xml file into a ArrayList, an bind the list.
It works, but it is possible to bind the xml File in a better way?
Thanks!
For this small XML (and not only) I would recommend that you take a look at JAXB. The two basic operations are marshalling (converting Java objects to XML data) and unmarshalling (converting XML data to Java objects) but verification and so on is also provided.

XML highcharts file creation using java

the question is how to create this xml file exactly for me to be able to use it in the get function of highcharts? The CSV file is created rapidly but now i want to create the xml file from the back end which is java.
<chart>
<categories>
<item>..</item>
</categories>
<series>
<name>xxx</name>
<data>
<point>1</point>
<point>2</point>
<point>3</point>
<point>4</point>
</data>
</series>
</chart>
Upon request:
<chart>
<categories>
<item>Apples</item> these two items must be populated from the database
<item>Pears</item>
</categories>
<series>
<name>John</name> this one also from database
<data>
<point>8</point> this is the data part, retrieved from database
<point>4</point>
<point>6</point>
<point>5</point>
</data>
</series>
</chart>
I want to create this type of xml structure using a java method or class where i retrieve all the information i need and then pass in into the xml file.
Anyways, am using xml to populate data for highcharts now.. Thanks for the answer

Categories

Resources