I'm marshaling objects into an XML file. How can I add comments into that XML file?
You can add comments right after the preamble with the proprietary Marshaller property com.sun.xml.bind.xmlHeaders (see XML Preamble Control)
In the included JAXB-implementation jdk1.6.0_29 the property is called "com.sun.xml.internal.bind.xmlHeaders"
See also question: How to add DOCTYPE and xml processing instructions when marshalling with JAXB?
So to get this XML with the test-comment after the preamble:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Test Comment -->
<player>
<name>Daniel</name>
<birthday>1982-06-09T00:00:00+02:00</birthday>
</player>
You can use this Java-Code:
JAXBContext context = JAXBContext.newInstance(Player.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.setProperty("com.sun.xml.internal.bind.xmlHeaders", "\n<!-- Test Comment -->");
m.marshal(player, System.out);
I do not see a way to do it with JAXB alone. However, I think you can leverage DOM to get the desired effect:
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document doc = builder.getDOMImplementation().createDocument(null, null, null);
final Binder<Node> binder = jaxbContext.createBinder();
binder.marshal(jaxbObject, doc);
final Comment comment = doc.createComment("This is a comment");
doc.appendChild(comment);
final DOMSource domSource = new DOMSource(doc);
// use System.out for testing
final StreamResult streamResult = new StreamResult(System.out);
final TransformerFactory tf = TransformerFactory.newInstance();
final Transformer serializer = tf.newTransformer();
serializer.transform(domSource, streamResult);
Where jaxbContext is the JAXBContext object you are working with and jaxbObject is the object to be marshalled. This sample just appends the comment to the end of the document. For a different location, you would have to traverse the DOM through the doc object or use XPath to find the exact element you want the comment added to and use appendChild on it.
If anyone comes to this now, like I just did, it is worth pointing out that the property to do this is now com.sun.xml.bind.xmlHeaders (no longer internal it seems), so you can solve the problem like this (I have only tried it with EclipseLink MOXY):
JAXBContext context = JAXBContext.newInstance(Player.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.setProperty("com.sun.xml.bind.xmlHeaders", "\n<!-- Test Comment -->");
m.marshal(player, System.out);
The following information originally comes from Marshaller Properties in the JAXB RI Extensions documentation on jaxb.java.net:
XML Preamble Control
This property allows you to specify an XML preamble (
declaration) and any additional PIs, comments, DOCTYPE declaration
that follows it. This property takes effect only when you are
marshalling to OutputStream, Writer, or StreamResult. Note that this
property interacts with the Marshaller.JAXB_FRAGMENT property. If that
property is untouched or set to false, then JAXB would always write
its XML preamble, so this property can be only used to write PIs,
comments, DOCTYPE, etc. On the other hand, if it is set to true, then
JAXB will not write its own XML preamble, so this property may contain
custom XML preamble.
Related
I want to know this so I can apply xsl transformations to the xml document without losing some entities like –
How do I tell the parser (any parser I dont care) which catalog to use and then execute the xsl transformations?, How do I connect the new configured parser to the transformation factory.
The code below represents the transformations I want to execute on the xml file (it works fine). I just want to know how can I add the XML Catalog approach so the xml-document loads correctly its DTD and continue with the xsl transformations steps.
try {
SAXTransformerFactory stf = (SAXTransformerFactory) TransformerFactory.newInstance();
Templates step1Template = stf.newTemplates(new StreamSource(
this.getClass().getResourceAsStream("xsltransformation_step1.xsl")
));
Templates step2Template = stf.newTemplates(new StreamSource(
this.getClass().getResourceAsStream("xsltransformation_step2.xsl")
));
Templates step3Template = stf.newTemplates(new StreamSource(
this.getClass().getResourceAsStream("xsltransformation_step3.xsl")
));
TransformerHandler th1 = stf.newTransformerHandler(step1Template);
TransformerHandler th2 = stf.newTransformerHandler(step2Template);
TransformerHandler th3 = stf.newTransformerHandler(step3Template);
StreamSource xmlStreamSource = new StreamSource(new File(xmlInputFile));
StreamResult outputStreamSource1 = new StreamResult(new File (outputNewFile1));
StreamResult outputStreamSource2 = new StreamResult(new File (outputNewFile2));
th1.setResult(new SAXResult(th2));
th2.setResult(new SAXResult(th3));
th3.setResult(outputStreamSource1);
Transformer t = stf.newTransformer();
t.transform(xmlStreamSource, new SAXResult(th1));
}catch (TransformerException e){
e.printStackTrace();
return false;
}
This is an example of the xmlInputFile containing entities
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE manual PUBLIC '-//docufy//Docufy Standard DTD 20080125//EN' '/system/cosimago/dtd/manual.dtd'>
<chapter>
<title>LEDs „5 – 8“ am CPU-Board prüfen</title>
<body>
<!-- just content -->
</body>
</chapter>
Please I would be really thankful if some good soul help me out with this.
Thank you in advance.
Andres
It's simplest to create your own XML parser (XMLReader) using SAXTransformerFactory.newInstance(). Then set the CatalogResolver on the parser using XMLReader.setEntityResolver(). Then wrap the XMLReader in a SAXSource, and supply this as the Source object to Transformer.transform().
With Saxon it's also possible to supply the entity resolver indirectly via a configuration property, but this is much more convoluted and is only needed if you aren't able to control the creation and configuration of the XMLReader yourself.
I'm having a problem generating a document with signed nodes. I have a bunch of signed XMLs, in text format. They signature is valid, I've tested it with xmlsec1. I have to load all the XMLs and put them in another XML document, for sending it to another service.
So, first of all I create the container document ("sobre" is a local variable, a JAXB root element):
JAXBContext context = JAXBContext.newInstance("org.importe.test");
StringWriter writer = new StringWriter();
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(sobre, writer);
String xml = writer.toString();
Document doc = loadXMLFromString(xml);
and then I add the XMLs to the container:
for (String cfexml : cfexmls) {
Document cfe = loadXMLFromString(cfexml);
Node newNode = doc.importNode(cfe.getDocumentElement(), true);
doc.getElementsByTagName("EnvioCFE").item(0).appendChild(newNode);
}
finally I get the xml from the container document:
TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
StringWriter outputWriter = new StringWriter();
trans.transform(new DOMSource(doc), new StreamResult(outputWriter));
String signedxml = outputWriter.toString();
The point is that I'm getting the child node modified, without namespaces, and so the signature validation of the extract node fails. Here is an excerpt of the XML that I've to add as a child node:
<?xml version="1.0" encoding="UTF-8"?>
<CFE xmlns="http://org.importe.test" xmlns:ns2="http://www.w3.org/2000/09/xmldsig#" xmlns:ns3="http://www.w3.org/2001/04/xmlenc#" version="1.0">
<data>
[...]
</data>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
[...]
</Signature>
</CFE>
but when the node is imported in the container document it get modified (actually I've noticed that it loose namespace declarations):
<?xml version="1.0" encoding="UTF-8"?>
<EnvioCFE xmlns="http://org.importe.test" xmlns:ns2="http://www.w3.org/2000/09/xmldsig#" xmlns:ns3="http://www.w3.org/2001/04/xmlenc#" version="1.0">
<Header version="1.0">
</Header>
<CFE version="1.0">
<data>
[...]
</data>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
[...]
</Signature>
</CFE>
</EnvioCFE>
How can I add a node to the container keeping the namespace declaration? More generally can I add it being sure it's added "as is", without any modifications?
(I'm using glassfish 4 and Java 7)
Thank you
EDIT:
This is the code of loadXMLFromString:
public Document loadXMLFromString(String xml) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new InputSource(new StringReader(xml)));
}
Ok, I've done research and I've found the problem:
if I use xalan-2.7.1.jar, xercesImpl-2.9.1.jar, xml-apis-1.3.04.jar and xmlsec-1.5.6.jar for DOM and XML security implementation, it works as I expect: adding a node does not modify the contents, so the digital signature is valid;
if I use the standard java libraries not only adding a node change the node itself, but sometimes the digital signature doesn't pass the xmlsec1 check, I imagine for a problem in the transformation from DOM to String.
I hope this can help someone, anyway I don't know why it happens, I'm sure I'm not doing weird things, but I've no time now to going deeply.
Actually I'm stuck to find how tell glassfish to load the DOM implementation I want, or to set a priority: I've found no way to say "load xerces instead of standard lib".
I am needing to program for offline transformation of XML documents.
I have been able to stop DTD network lookups when loading the original XML file with the following :
DocumentBuilderFactory factory;
factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(true);
factory.setFeature("http://xml.org/sax/features/namespaces", false);
factory.setFeature("http://xml.org/sax/features/validation", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
// open up the xml document
docbuilder = factory.newDocumentBuilder();
doc = docbuilder.parse(new FileInputStream(m_strFilePath));
However, I am unable to apply this to the TransformerFactory object.
The DTDs are available locally, but I do not know how to direct the transformer to look at the local files as opposed to trying to do a network lookup.
From what I can see, the transformer needs these documents to correctly do the transformation.
For information, I am transforming MusicXML documents from Partwise to Timewise.
As you have probably guessed, XSLT is not my strong point (far from it).
Do I need to modify the XSLT files to reference local files, or can this be done differently ?
Further to the comments below, here is an excerpt of the xsl file. It is the only place that I see which refers to an external file :
<!--
XML output, with a DOCTYPE refering the timewise DTD.
Here we use the full Internet URL.
-->
<xsl:output method="xml" indent="yes" encoding="UTF-8"
omit-xml-declaration="no" standalone="no"
doctype-system="http://www.musicxml.org/dtds/timewise.dtd"
doctype-public="-//Recordare//DTD MusicXML 2.0 Timewise//EN" />
Is the mentioned technique valid for this also ?
The DTD file contains references to a number of MOD files like this :
<!ENTITY % layout PUBLIC
"-//Recordare//ELEMENTS MusicXML 2.0 Layout//EN"
"layout.mod">
I presume that these files will also be imported in turn also.
Ok, here is the answer which works for me.
1st step : load the original document, turning off validation and dtd loading within the factory.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// stop the network loading of DTD files
factory.setValidating(false);
factory.setNamespaceAware(true);
factory.setFeature("http://xml.org/sax/features/namespaces", false);
factory.setFeature("http://xml.org/sax/features/validation", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
// open up the xml document
DocumentBuilder docbuilder = factory.newDocumentBuilder();
Document doc = docbuilder.parse(new FileInputStream(m_strFilePath));
2nd step : Now that I have got the document in memory ... and after having detected that I need to transform it -
TransformerFactory transformfactory = TransformerFactory.newInstance();
Templates xsl = transformfactory.newTemplates(new StreamSource(new FileInputStream((String)m_XslFile)));
Transformer transformer = xsl.newTransformer();
Document newdoc = docbuilder.newDocument();
Result XmlResult = new DOMResult(newdoc);
// now transform
transformer.transform(
new DOMSource(doc.getDocumentElement()),
XmlResult);
I needed to do this as I have further processing going on afterwards and did not want the overhead of outputting to file and reloading.
Little explanation :
The trick is to use the original DOM object which has had all the validation features turned off. You can see this here :
transformer.transform(
new DOMSource(doc.getDocumentElement()), // <<-----
XmlResult);
This has been tested with network access TURNED OFF.
So I know that there are no more network lookups.
However, if the DTDs, MODs, etc are available locally, then, as per the suggestions, the use of an EntityResolver is the answer. This to be applied, again, to the original docbuilder object.
I now have a transformed document stored in newdoc, ready to play with.
I hope this will help others.
You can use a library like Apache xml-commons-resolver and write a catalog file to map web URLs to your local copy of the relevant files. To wire this catalog up to the transformer mechanism you would need to use a SAXSource instead of a StreamSource as the source of your stylesheet:
SAXSource styleSource = new SAXSource(new InputSource("file:/path/to/stylesheet.xsl"));
CatalogResolver resolver = new CatalogResolver();
styleSource.getXMLReader().setEntityResolver(resolver);
TransformerFactory tf = TransformerFactory.newInstance();
tf.setURIResolver(resolver);
Transformer transformer = tf.newTransformer(styleSource);
The usual way to do this in Java is to use an LSResourceResolver to resolve the system ID (and/or public ID) to your local file. This is documented at http://docs.oracle.com/javase/7/docs/api/org/w3c/dom/ls/LSResourceResolver.html. You shouldn't need anything outside of standard Java XML parser features to get this working.
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());
Right now I am getting this as an XML output from my JAXB Marshaller
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><create></create>
But I want my root element as:
<create xmlns="http://ws.abc.com" xmlns:doc="http://ws.abc.com">
Do I need to modify this using parsers, Or is there any annotation available.
You can set the following property on the Marshaller to remove the header:
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
For More Information
http://blog.bdoughan.com/2011/08/jaxb-and-java-io-files-streams-readers.html
I've used a Transformer in the past. You'd want something like the following sample code:
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StreamResult transformedDoc = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(content); // Where content is a org.w3c.dom.Document object.
transformer.transform(source, transformedDoc);
So maybe do your marshalling and then process. Not sure if this is the best approach but it would work.