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.
Related
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...
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.
I use DOM parser to read data from XML file. I know how to read, modify and write back the data. However, I would like to know if it is possible to create an object from an XML file.
I have an XML file which looks like this:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE people SYSTEM "validator.dtd">
<people>
<student>
<name>John</name>
<course>Computer Technology</course>
<semester>6</semester>
<scheme>E</scheme>
</student>
<student>
<name>Foo</name>
<course>Industrial Electronics</course>
<semester>6</semester>
<scheme>E</scheme>
</student>
</people>
and I would like to make it an objects out of it so I can pass them around. Does a solution exist ?
Yes. This is possible through JAXB (Java API for XML binding)
All JAXB implementations provide a tool called a binding compiler to bind a XML schema in order to generate the corresponding Java classes.
For details refer: http://www.oracle.com/technetwork/articles/javase/index-140168.html#xmp1
You could have a look at XML beans or JAXB libraries. In case you don't have a schema file but have a sample XML file, you could create one using inst2xsd tool of xmlbeans. http://xmlbeans.apache.org/docs/2.0.0/guide/tools.html. This could get you started with the schema.
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.
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