Jaxp xml parsing & validation without writing to file - java

I've a JMS messaging app thats reading and writing to MQ queues. The message data is string form and in xml format (minus the normal header markers like the xml version etc). I'm looking at the best ways to read in, write out and validate against an xsd schema however the examples I'm coming across all talk about working with files.
Is there any way (tutorials out there) to take an xml string; read it in and validate it and also do the same for an xml string I create validate and write out without writing to disk?
Would appreciate any pointers.

Check out the javax.xml.validation APIs in Java SE, in particular the Validator class which is used to validate XML content against an XML schema:
http://download-llnw.oracle.com/javase/6/docs/api/javax/xml/validation/package-summary.html

Use a StringReader on the strings, pass the reader to the JAXB methods to read the contents.

thanks folks I managed to sort this one out with the following.
Marshall:
JAXBContext jaxbContext = JAXUtility.getContext(packageLocation);
StringWriter sw = new StringWriter();
Marshaller m = jaxbContext.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
if (o instanceof UnadvisedDeal) { m.marshal((UnadvisedDeal) o,sw);
UnMarshall:
JAXBContext jaxbContext = JAXUtility.getContext(packageLocation);
Unmarshaller um = jaxbContext.createUnmarshaller();
ud = (UnadvisedDeal) um.unmarshal(new StringReader(sw.toString()));
thanks for the help though

Related

EXI get JAXB unmarshaller

I wish to know the EXI equivalent of the JAXB unmarshaller.
I have looked at the EXI examples, where I have successfully obtained EXIFactory, set the grammar, get the XMLReader.
The example then creates a transformer to transform EXI stream to XML stream.
However, I do not need the output stream. I just need the unmarshalled result to stay as in-memory POJOs. I need the result to be direct unmarshall of EXI. I am using EXI marshall/unmarshall as a faster alternative to text XML.
Forgot to say which library I was using. Here it is:
<groupId>com.siemens.ct.exi</groupId>
<artifactId>exificient</artifactId>
<version>0.9.6</version>
JAXB Marshaller/Unmarshaller let you set various input/output mechanism
e.g.
Unmarshaller.unmarshal( javax.xml.transform.Source source )
or
Marshaller.marshal( Object jaxbElement, javax.xml.transform.Result result )
EXIficient implements
javax.xml.transform.Source (see com.siemens.ct.exi.api.sax.EXISource)
javax.xml.transform.Result (see com.siemens.ct.exi.api.sax.EXIResult)
Both, EXISource and EXIResult, can be initialized with the EXIFactory.
Hope this helps,
-- Daniel

Create dynamic xml from properties file?

I have decided to create dynamic xml as a response of my rest service.
Xml structure is defined in properties file it may change in future.
What will best approach to achieve this task.
Help me out with suggestions friends.
Thanks in advance
It is not recommended to use the properties file for generating dynamic XML. If the client requirement is that you have to used that properties file. Else the recommended way is to use the XSD schema generation method.
You can use the javax.xml.stream.XMLOutputFactory to generate the XML output. You can read the XML structure from the property file as your requirement and can generate the output using the javax.xml.stream.XMLOutputFactory.
Hope following code will helpful to you.
StringWriter stringWriter = new StringWriter();
XMLOutputFactory xmlFactory = XMLOutputFactory.newFactory();
XMLStreamWriter writer = xmlFactory.createXMLStreamWriter(stringWriter);
writer.writeStartDocument();
writer.writeStartElement(<<First element>>);
Using the Properties propertyNames() we will be getting the list of all the keys.
Using keys we can find the values.
Using this we can create a xml file using StringBuilder
For Example
StringBuilder sb = new StringBuilder();
sb.append("<"+key+">");
sb.append("+value+");
sb.append("</"+key+">");
After this write it to a file . Using FileOutputStream.java

Saving a project into an XML file

I'm writing a game in java using Netbeans.
I want to be able to save the game to an XML file in the beginning of each round, and to be able to load any saved game in the beginning of the game.
The XML file should eventually include the current state of the game when saved (players, names, sum of money, etc.).
I read over the internet and understood that I need to create a content tree of all the classes of the game using DOM and then Marshall the tree into an XML file, using JAXB.
I have no idea where to start from, how to create the context tree, and so on.
Any help or good tutorial would be helpful (couldn't find anything good).
This is a broad question, however if you do not know how to read/write XML from java beans using jaxb take a look at this tutorial http://www.vogella.com/tutorials/JAXB/article.html. I would start there before trying to make a game.
JAXB enables you to easily convert instances of Java objects to/from XML. With JAXB you will never need to interact directly with DOM.
JAXBContext jc = JAXBContext.newInstance(Game.class);
File file = new File("gameData.xml");
Unmarshaller unmarshaller = jc.createUnmarshaller();
Game game = (Game) unmarshaller.unmarshal(file);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(game, file);
JAXB is configuration by exception meaning you only need to annotate your model where you wan the XML representation to differ from the default. The following will help you get started:
https://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted

JAXB generates unformatted XML using XMLEventWriter

I'm using JAXB in order to generate XML files, and due to a business need I'm currently writing it to the middle of some other XML file using XMLEventWriter:
marshaller.marshal(jaxbElement, xmlEventWriter);
And currently setting some properties like:
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8");
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
But, besides having the JAXB_FORMATTED_OUTPUT set to true, my XML is not being formatted.
Does anyone knows what may be the problem?
This only happens when I use the XMLEventWriter...
Thanks in advance.
When you are using an XMLEventWriter as a sink, the JAXB marshaller is only in charge of sending the appropriate XML events to it and the XMLEventWriter may still choose to write out unformatted XML. My advice is to check the configuration of your XMLEventWriter in addition to Marshaller.
Unfortunately, the default XmlEventWriter implementation does not indent. The stax-utils library provides a IndentingXMLEventWriter which might help in these cases.

XML Schema with enumeration

I am using XJC to create from XML Schema a Jaxb annotated class.
With
JAXBContext context = JAXBContext.newInstance(SomeClass.class);
Marshaller marshaller = context.createMarshaller();
StringWriter writer = new StringWriter();
marshaller.marshal(cardJob, writer);
String resultString = writer.getBuffer().toString();
I create an xml output string - This works perfect. What I want to achieve is, adding an enum to this class like
Jan (1, "January"),
Feb (2, "February"),
...
by creating an entry in the xml schema and getting in the end
an output like
<.January><./January>.
Maybe wrong but something like an xml element from an enum entry name.
Help would be pretty great, cause I dont get any solution :(
maybe you can build your own ContentHandler
org.xml.sax.ContentHandler
handle the enums and put them into buffer before endDocument
marshaller.marshal(obj, new yourContentHandler());

Categories