i would like to know how to parse xml file in java and convert results to javabean from it. Can anyone guide me thanks?
A really simple and convenient way: http://simple.sourceforge.net/
You can find many examples there for using it, too.
JAXB allows you to write an XSD to which your XML file should conform and then generate the java beans automatically. You don't need to hand-code the beans yourselves.
Check out the tutorial at http://docs.oracle.com/javase/tutorial/jaxb/intro/.
Related
I have XML Configuration file and I need to load/save it so What are best ways to Parse XML Files for Load/Save configurations other than Serialization as I don't like the option of Serialization
For a Java application I serialized a pretty complex application state using XStream. For the most part this works extremely good. If the object is really simple this should not be a problem. Another simple alternative I often use to transfer data from Object <-> XML is JAXB with annotations on the Objects, or if the XML structure is the master with an XML Schema from which the classes can be generated.
For C# you could use IXmlSerializable, or DataContract. Multiple answers can be found on SO using both these classes. See this to see how to use DataContract.
I'm using Apache Commons Digester (with annotations) in order to load an XML file into a Java class.
Everything works correctly.
Now, I need to update the XML file. I have to change (in Java) the value of a property, and then to write out the new XML file.
How could I do? As far as I can see, Digester API is not designed for this purpose.
Edit: reading the answers, I understand that I did not give enough informations. My XML file is a configuration file for a program A, so I really need its content when I launch program A. Then, I have another GUI program B that is able to modify this configuration file, it just takes some input from the user and modifies the relative fields on the XML file.
As you have found, Digester is a read-only tool - it provides a mapping from XML to Java classes, but not the reverse. If you need to read the XML into Java classes and then write it back to XML again, I would suggest either:
Keep using Digester for the reading, and use a low-level XML writer
class (such as XMLStreamWriter)
to write the data back. This is suitable if your data is not that
complex, and/or the output XML is a different structure to the input
XML.
Replace Digester with a full Java to XML mapping library (JAXB, JiBX
etc.), which will both read and write the XML for you. This is
suitable if your data is more complex, and/or the output XML is the
same structure as the input XML.
Don't know enough about what you are doing to really recommend one of those approaches over the other.
If you don't actually need the data in Java classes at all and are just trying to transform it, then XSLT as #sharonbn says is also a good solution.
XML modification (usually called XML transformation) is best handled in XSLT standard. Apache Xalan is (one of) the Java libraries that implement this standard
I need to parse and modify the XML in android ..Can any one suggest which XML parser is better to parse and modify the XML in android ..?
Currently I'm using XMLpullparser but using this i'm not able to modify the XML...
Xpath is available for Android developers I believe. I use that all the time for any XML parsing really.
If the XML has a simple structure then you can deserialize the file into an object. You modify some properties of that object and serialize it to XML afterwards.
XStream is a simple library to serialize objects to XML and back again. It can be found here.
I think this is a clean way, but it isn't the easiest way if the XML file is very complex (because you have to map its structure to a Java class).
XMLPullParser isn't really designed for complex tasks AFAIK. SAX Parser is much better for advanced tasks, but it's also not as easy to use.
For manipulating XMLs, you could use SAX Filters. Look at this tutorial (IBM tutorials are great!): http://www.ibm.com/developerworks/xml/library/x-tipsaxfilter/
I want to create a Java bean from the XML file which I will get as input at runtime. Can anyone please suggest me the generic way for doing the XML to Java bean conversion?
Typically java classes can be generated from XSD. So, if you do not have it yet you should first write it manually or generate from your XML sample. There are many tools that do this. I know for example XMLSpy But I believe that a lot of others exist.
Then you should generate your java classes. The are many ways to do this too. For example JAXB (as it was mentioned by #Chris Jester-Young) or Castor.
How can I get java classes from an xml file?
In this situation, I don't have an XML schema and this way I can't use JAXB, Castor or other xml binding API as far as I know.
You can generate schema from XML file using certain tools. Then, use Apache XMLBEANS to create your classes.
XStream is great for XML -> objects and vice versa. Fast, lightweight, and works well without any schema.
Altova is also the best to generate java Classes from XML/XSD