开发者

Jaxp xml parsing & validation without writing to file

开发者 https://www.devze.com 2023-01-08 17:37 出处:网络
I\'ve a JMS messaging app thats reading and writing to MQ qu开发者_如何学Pythoneues. The message data is string form and in xml format (minus the normal header markers like the xml version etc). I\'m

I've a JMS messaging app thats reading and writing to MQ qu开发者_如何学Pythoneues. 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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号