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.
Related
Generate java file from XML file without any API
Hi, I would like to know if it's possible to generate a java class from existing model as illustrate in this picture below.enter image description here
Informations about the generated class will be found in a XML file like this:
<?xml version="1.0" encoding="UTF-8"?>
<entity name="Balise1">
<attribut name="a" type="List" basetypeid="1"/>
<typedef type="List" basetypeid="2" id="1"/>
<typedef type="String" id="2"/>
</entity>
For exemple when I find the tagname "entity" I create a instance of MjEntity and so on. After iterating the XML file, I have just to generate my Java class.
I believe a costum XML format reader requires you to write your own generator.
There are formats that can generate Java classes from XML. Take a look at .XSD -> .java here..
Generate Java classes from .XSD files...?
Also, if you dont want entity relations in the class object, you can create an orm.xml (Object Relation Mapping) to define this. Take a look at
https://dzone.com/articles/persisting-entity-classes
I have to output an XML file which can contain use amount of data, I am using DOM parser to write XML file. It is also possible to append data to an existing XML file.
My requirement is add data to the root element.
Is it possible to append data without reading the entire XML document (Not to load XML into memory)?
Example Data:
Current XML file:
<employees>
<employee>
<name>jon</name>
<age> 22</age>
<address> address1 </address>
</employee>
</employees>
Required file:
<employees>
<employee>
<name>jon</name>
<age> 22</age>
<address> address1 </address>
</employee>
<employee>
<name>jon1</name>
<age> 24</age>
<address> address2 </address>
</employee>
</employees>
It would be hard if you don't want to load entire XML into memory.
You can achieve this by manipulating raw String (substring, etc.) - but I don't recommend this.
Or you can try using SAX reader http://www.saxproject.org/apidoc/org/xml/sax/XMLReader.html which enables you to manipulate XMLs "on the go". (I'm sorry, although you can use SAX parsers to process XML without reading its whole content, you cannot edit with it)
EDIT:
On second though you can copy existing XML using SAX parser, and by adding event listener to e.g. root you can add a child. It might be good solution if your concern is memory (big xml file).
You could use DOM4j for doing that.
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.
I have some XML data inside an XML file which i want to pass to another application. I have used XMLPullPaser.
<?xml version="1.0" encoding="utf-8" ?>
<node1>
<node2>dd03</node2>
<node3>,17,0,,**<xml><cell>555</cell></xml>**,</node3>
</bintextobj>
node 3 contains the data as highlighted. The xml I want to pass as data of xml file. Is there any way of achieving this?
Use CDATA to store the value. This will cause the parser to treat the value of node3 like plain text.
<node3><![CDATA[,17,0,,**<xml><cell>555</cell></xml>**,]]></node3>
write xsl to extract the <xml>..</xml> and write into the new xml file which can be passed to the other applications.
can you help me in parsing xml with nested <?xml version="1.0" encoding="utf-8"?> tags. when i am trying to parse this xml, i m getting parsing error.
<?xml version="1.0" encoding="utf-8"?>
<soap>
<soapenvBody>
<serviceResponse>
<?xml version="1.0" encoding="UTF-8"?>
<data>
<respCode>0</respCode>
</data>
</serviceResponse>
</soapenvBody>
</soap>
I don't think this is really a Java problem. Having a second XML declaration within the XML body is just illegal, so I don't think you'll be able to get any XML parsers to parse that. If you have control over the XML (it looks like you're generating it to store a response) then you could try wrapping the inner-XML document with CDATA:
<?xml version="1.0" encoding="utf-8"?>
<soap>
<soapenvBody>
<serviceResponse>
<![CDATA[
<?xml version="1.0" encoding="UTF-8"?>
<data>
<respCode>0</respCode>
</data>
]]>
</serviceResponse>
</soapenvBody>
</soap>
EDIT:
I'm thinking that you most likely don't want the extra XML declaration inside that response at all. Do you have control over the code that creates the response? My guess is that the XML snippet <data>...</data> is created as a separate DOM object and then the string is spliced in the middle of the response. Writing out the entire XML document object results in the XML declaration being included, but if you just grab the document root node object (<data>) and write that out as a string then it probably won't include the extra XML declaration that's causing you all this trouble.
It occurred to me that a parser made for dealing with HTML might be able to do what you want. Since HTML tends to be a total mess compared to strict XML, HTML parsers are usually much more error-tolerant. A quick search turned up jsoup. I was able to pull the respCode from your sample XML above with roughly this code:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
String data = "your xml goes here";
Document doc = Jsoup.parse(data);
String respCodeRaw = doc.select("respCode").first().text();
int respCode = Integer.valueOf(respCodeRaw);
(I actually tested the library in the Clojure repl, but the code above should work!)
A tag that starts with like <? is a processing instruction. <?xml...> is an XML declaration, and can only be present at the beginning of the xml content. It's not allowed in the XML body.
Why does your soap body contain this? Do you have the option of removing it?
i did not find any parser in java to parse such embedded xml as it is not a valid xml and i guess almost all parses validate the xml before parsing it. so i choose the option to preprocess the xml and selected the inner xml then using SAX parser i parsed the xml and retrieved the values from xml. Guys thanks for your replies.