Framework to validate XML order against XSD - java

I am looking for a Java framework which can validate order of elements in XML against XSD. I have already tried JDK 5's javax.xml.Validate and popular article http://www.ibm.com/developerworks/xml/library/x-javaxmlvalidapi/index.html
But none of it gave me specific result.These implementation one way or another told me that xml is invalid but could not provide me specific error description.

maybe take a look at this page:
https://www.java-tips.org/java-ee-tips-100042/175-java-architecture-for-xml-binding/1833-what-is-new-in-jaxb-20.html
You can do nearly everything in XML with the JAXB

Related

Best options for - Spring XML parsing to read certain elements

I have read the spring documentation for reading/writing the XML documents..(http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/oxm.html)
The options provided using different typs of marshalling(Oxm, Jaxb, jibx, castor, xstream) all seems to be using the conversion of a document based on some mapping file or a java bean or identifying by comparing each individual element using xstream.
I have a requirement, where i will get big xml files and i need to read some of the elements only in it and i dont want to keep the complete xml into the memory.
What are the options that i have in spring?
if any of the options that the spring documentation provides and useful for my requirement, please provide some examples.
BTW, i am using latest spring 4... FYI.
For large XML file you would need a SAX parser which is an event based parser.
Here an example
http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/
Spring Object/XML Mapping is to convert XML to and from an object which is a different use case.

Custom StAX Parser for XML using javax wrappers

Custom StAX Parser for XML using javax wrappers
How do you do this; or at least good suggestions on the right documentation / examples / tutorials?
I've been using the javax.xml.stream package to process XML files but the application is begging for some "non-standard XML" (easy to understand what the means if you're not picky). I can write the parser, but I want this to be configurable: so that the app continues to use the same XML processing code except for changing the parser as needed.
The hard part at this point is finding concrete info on how this is done. Documentation speaks of, for example, configuring the parameters of SAXParserFactory and such, but I haven't found specific documentation or examples. I've even looked into some existing StAX source code. Need some good hints / guidance on how this is done in order to move forward.
According to the documentation, you can't. You can use one of three approved parsers. Anything else will result in an error.

Parsing XML response from RESTful service in java

Please bear with me for this novice question.
I am calling a RESTful web service APIs that returns XML response. Apart from normal XML parsing schemes like DOM based parsing, SAX based parsing, is there a way to transform this XML response directly into some object? What more details/specification from service side would be required to do such transformation?
i can't give a summary of all the options available, but i recently used jaxb to do the opposite (java to xml) and it was simple and easy to use. since jaxb also supports xml to java, as described here, i would suggest giving that a look. it's based on annotations and java beans (or pojos) - you just indicate which attributes correspond to the elements with attributions, and it does the rest.
if you have a schema, it will generate java classes for you. alternatively, here's an example of working without a schema.
ps according to comments in the final link, you don't even need to annotate if the names match the xml!

Generic XML to Java bean Conversion

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.

What is best practice in converting XML to Java object?

I need to convert XML data to Java objects. What would be best practice to convert this XML data to object?
Idea is to fetch data via a web service (it doesn't use WSDL, just HTTP GET queries, so I cannot use any framework) and answers are in XML. What would be best practice to handle this situation?
JAXB is a standard API for doing this: http://java.sun.com/developer/technicalArticles/WebServices/jaxb/
Have a look at XStream. It might not be the quickest, but it is one of the most user friendly and straightforward converters in Java, especially if your model is not complex.
For a JMS project we were marshalling and unmarshalling (going from java to xml and xml to java) XML embedded in TextMessages (string property). We tried JAXB, Jibx, and XMLBeans. We found that XMLBeans worked best for us. Fast, easily configurable, good documentation, and easy Maven integration.
I have used and will continue to use JDOM -> www.jdom.org
Another option is a Sax Parser. It is procedural - i.e. a visitor pattern - but if the xml is fairly lightweight, (and even medium weight) I have found it to be very useful for this.
JAXB API which comes in Java(In built).
I have used JIBX in MQ module. It works very well. Ant config is simple. Used Xsd2Jibx converter to generate the binding files and Java beans from XML schema. Marshalling and un-marshalling allow to specify character-set parameter. It was useful in my project to handle custom character-set. But I found an issue in the binding compiler. If the Java bean has lengthier path name, it generates class file with lengthier file name which will cause issue in Windows XP(it has a maximum file length limit).
I haven't used other APIs. So I am not trying to compare with others. If you decided to use JIBX, I hope this will be helpful.
More details, please refer JIBX website
I've used XStream as well, it is easy to use and customizable. You can add your own custom converters and that was very handy for me...
So surprised more people have not mentioned Jibx. Amazing lib and i think a lot simpler to use than Jaxb. Performance is also fab!
For this you can also consider apache's bitwixt and simple framework for xml

Categories