I am getting object of type org.w3c.dom.Document from a source (basically a parsed xml document - java object). I want to use this xml document object to create java objects using JAXB. How can I do that?
You can unmarshal directly from DOM objects, this includes a org.w3c.dom.Document:
http://docs.oracle.com/javase/6/docs/api/javax/xml/bind/Unmarshaller.html#unmarshal%28org.w3c.dom.Node%29
If you need to specify the type of object you are unmarshalling, then you can wrap the DOM node in a javax.xml.transform.dom.DOMSource and use the following API:
http://docs.oracle.com/javase/6/docs/api/javax/xml/bind/Unmarshaller.html#unmarshal%28javax.xml.transform.Source,%20java.lang.Class%29
From the top of my head you could just convert the Document to a Stream and use the Unmarshaller to convert to the JAXB instance you need.
Something like:
MyJAXB o = (MyJAXB)unmarshaller.unmarshall(new StringReader(arrayoutsream.tostring()));
Disclaimer: Haven't even bothered to see if this compiles but you should understand how to approach this
Related
I recently got an old XML over HTTP API. It has few response types and all those responses have no namespace or type attributes. They all have the same root node and then different set of child nodes.
Is there a way in java to UnMarshall such XMLs ? It would be like using child nodes as discriminator fields. Two sample responses are given below.
<Response>
<A1/>
<A2/>
</Response>
<Response>
<B1/>
<B2/>
</Response>
The best approach really depends on what you want to do. If you just want to unmarshal the data you could define a model using JAXB, for instance, which includes all the potential child elements. Then when you unmarshalled an instance documents only the child elements actually present in the document would have values.
If you instead want to have separate models for the different response variations your best approach would be to use a BufferedInputStream and call mark() at the start, then read enough of the document with a pull parser such as XMLStreamReader to determine the actual response type. Then you can reset() the stream to the start of the document and start over using JAXB with the appropriate data model.
I have a Java object that uses XStream to convert the object from XML to a java object and vice versa.
I know how to convert the Java object to an XML document but i don't know how to do this without having to initialize the attributes in the XStream object. I.e. if the attribute is not initialised XStream does not generate the XML tag when it generates the XML.
What i would like to do is to generate an XSD from that Java object and to be able to do that i need an XML version of the object containing all the possible tags.
I cant really initialize each individual attribute because the XStream object has hundreds of attributes with several child attributes (i.e. a very complex structure).
Is it possible to generate the XML from an XStream object that contains all the possible tags (probably with randomized data) without having to initialize all the attributes in the XStream object?
It is probably programmable but i will have to iterate through each attribute, and each attribute's attribute and each attribute's attribute's attribute (you get the picture) to try and work out what all the tags are :)
Edit
I am also thinking that if it is possible to convert the XStream object to a JAXB object i might be able to generate the XSD as i understand it is easier to generate an XSD from JAXB. Is this the easier approach?
I was wondering how I can pass a parsed xml in different java classes within the same project. For example, I have
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder builder = factory. newDocumentBuilder();
Document document = builder.parse("Companies.xml");
etc.
And I can parse through it and do what I need to do in main, but I would like to be able to split up this process into different classes. I would have a class for employees, departments, etc. But when I do this in main I can simply say,
NodeList EmployeeList = document.getElementsByTagName("employees");. But if I were to try this in a different class it wouldnt have access to document. So how can I use the document in a different class, or do I have to create a new "document" file in every class? Thanks!
You would pass in the Document object to the constructor of the new class. Then it can parse the elements it needs.
Alternatively you could use a Java Object parser that can convert to and from XML for you.
Simple XML
Best would be
1.) To have a constructor in each class, accepting the Document Object as argument.
2.) Use the Document Object in other classes to get the values.
3.) Or you can directly convert from XML to JAVA Object or Java Object to XML, follow these links.
link1 and link2
I'm developing a plugin that has node(computer) objects with attributes like:
String name
String description
String labels
Launcher computerLauncher
...
I can convert the node(computer) object to an XML-formated String like:
String xml = jenkins.instance.toXML(node);
Which gives me a string:
<name>Computer1</name>
<description>This is a description</description>
<labels>label1 label2</labels>
<launcher>windows.object.launcher.12da1</launcher>
Then I can go the other way back:
Node node = jenkins.instance.fromXML(xml);
I have no methods for changing attributes in a Node so I want to convert it to XML, change som attributes and then make it a Node again.
I see two options
Manipulate the XML with some String methods to replace everything in between the <> tags.
Try to cast the XML string to something like a real Object and manipulate it that way.
Not sure what would be the best approach.
Why invent something new when there already is support for all that using Java's DOM (Document Object Model) API?
Use a DocumentBuilderFactory to get a DocumentBuilder and create a Document instance. With this you can create the 'Node' objects (please note that the example you posted is actually not valid XML, it's missing a root node) in your toXML method, serializing the Document to a String could be done by using a Transformer.
With the DOM API you can also modify the attributes of your existing elements.
Parsing the Document instance from an XML string is realized again with the help of the DocumentBuilder, using DocumentBuilder#parse.
If your DOM operations are not too complex this should be a nice, quick way to accomplish your goal.
It makes sense to me to use a DOM-like approach. But don't use DOM itself: there are much better alternatives like JDOM and XOM that have much friendlier APIs.
recenty I had to manipulate large XML files (my software had to create some XML files dynamically and get input data from some other XML files). To do this I've used JAXB, which is a very neat API that marshalls XML files into Java objects and Java objects into XML files automatically.
However to do this I had to create a XSD file to specify the XMLs that I would need to read and write from.
Therefore JAXB requires more work to set up than DOM, so if your needs are simple I suggest that you use DOM, however if your needs are more complex, then I would suggest JAXB.
I have a org.w3c.dom.Document parsed by org.dom4j.io.DOMReader.
I want to search the dom4j DOM document via org.w3c.dom.Element.
say, I have a org.w3c.dom.Element that I need to locate on the dom4j document. I don't have any element information except for having the org.w3c.dom.Element object as a parameter.
I want something like dom4j doc.findByDomElement(org.w3c.dom.Element element) which would return a org.dom4j.Element.
What you need isn't provided by dom4j out of the box.
Dom4j allows you to parse an org.w3c.dom.Document as an org.dom4j.Document, but then you cannot search through the dom4j Document by an org.w3c.dom.Element; you should do that on your own method. For instance, you can use xpath to search some nodes. Also, dom4j provides org.dom4j.dom.DOMNodeHelper class which is a collection of utility methods to do some conversion from org.w3c.dom objects to org.dom4j objects but I've not found the method you need. Take a look here. In addition, dom4j provides the org.dom4j.io.DOMWriter class to do the opposite of org.dom4j.io.DOMReader.
When you say:
I don't have any element information
except for having the
org.w3c.dom.Element object as a
parameter.
Well, I think the Element object contains all the informations that you need to search manually through the dom4j tree.
Finally, I'd like to suggest you to use only one library to handle xml in your code. Do you have any particular needs? Why are you using both org.dom4j.Document and org.w3c.dom.Document?