How to use getDocumentElement in user-defined class? - java

I am implementing a class that reads XML files and calls methods related to the tags. I have coded this in C++ and all I had to do was inherit something like wxXmlDocument inorder to get the root of my Xml.
I want to do this in Java, and the class that can get me the root element is the interface Document. I want to be able to call getDocumentElement() without having to implement it. Can anyone tell me how?
Thanks. :)

This is the simplest example of using the built-in DOM parser:
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
final DocumentBuilder db = dbf.newDocumentBuilder();
final Document doc = db.parse(new File(filename));
Note that the DOM version of parsing will load the entire document into memory. If you are dealing with very large documents (compared to main heap available) you will likely want to learn how to use the SAX or STAX versions of parsing.

Related

Java using a parsed xml in different classes

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

JAXB and document object

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

XML formatted Java string manipulation

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.

find by org.w3c.dom.Element object as parameter on org.dom4j.Document (convert org.w3c.dom.Element to org.dom4j.Element)

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?

Simple way to do Xml in Java

Is there is Simple way to read and write Xml in Java?
I've used a SAX parser before but I remember it being unintuitive, I've looked at a couple of tutorials for JAXB and it just looks complicated.
I don't know if I've been spoilt by C#'s XmlDocument class, but All I want to do is create an Xml Document that represents a a set of classes and their members (some are attributes some are elements).
I would look into serialization but the XML has to have the same format as the output of a c# app which I am reverse engineering into Java.
I recommend XOM. Its API is clear and intuitive.
You should check out Xstream. There is a 2 minute tutorial that is really simple. To get the same format, you would model the classes the same.
If you are using jdk 1.4 or newer take a look at XMLEncoder class.
Some of the more popular approaches to consider:
Java Archictecture for XML Binding
JAXB is a specification for a standard XML binding. If you already have an XSD, it can generate your Java classes for you, and then all that's left is to use a standard API for marshalling/unmarshalling.
Reference implementation from Glassfish
Apache's implementation JaxMe
Other binding approaches
As with JAXB, these approaches use XML-based binding configurations. They may provide more fine grained control of the unmarshalling process.
Castor
JIBX
Roll your own
Using StAX
Using XOM
Using plain XPath
Dom4j is a simple api for creating xml documents in java.
Document document = DocumentHelper.createDocument();
Element root = document.addElement( "root" );
Element author2 = root.addElement( "author" )
.addAttribute( "name", "Toby" )
.addAttribute( "location", "Germany" )
.addText( "Tobias Rademacher" );
The most simple way so far is the MarkupBuilder in Groovy. Think of Groovy as a new syntax for Java. The XmlSlurper can be used to read XML.
I think that Apache XMLBeans provides the functionality you are after.
The Wikipedia page gives a good overview and example usage.
There is a wide choice of XML processing options for Java, though judging from the .NET documentation for XmlDocument, the Java DOM implementation is the closest out-of-the-box equivalent.
.NET XmlDocument:
This class implements the W3C Document
Object Model (DOM) Level 1 Core and
the Core DOM Level 2.
Java Document:
See also the Document Object Model (DOM) Level 3 Core Specification.
Sample code:
public static void main(String[] args) throws Exception {
File xmlFile = new File(".classpath");
// read it
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(xmlFile);
// walk it
System.out.println("Node count=" + countNodes(document));
// write it
Source source = new DOMSource(document);
Result result = new StreamResult(System.out);
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(source, result);
}
/** Doesn't count attributes, etc */
private static int countNodes(Node node) {
int count = 0;
NodeList kids = node.getChildNodes();
count += kids.getLength();
for (int i = 0; i < kids.getLength(); i++) {
count += countNodes(kids.item(i));
}
return count;
}
I think JAXB is only complicated if you look at wrong examples. Specifically, yes, schema-based way can get messy. But code-first, annotation-based is trivially easy.
Another easy alternative is XStream. And for non-binding case, StaxMate, which is an add-on for streaming Stax parsers.
If SAX parsing is mandatory, JAXP is a good choice. I prefer DOM parsing and use jdom which seems a lot easier to me.
I would certainly use XOM if you want a DOM-like approach and SAX (www.sax.org) if you want a SAX-like approach. I was involved in the early development of XML and SAX was developed as an event-driven approach, which is useful for some applications. DOM/XOM and SAX are complementary - sometimes you need one, sometimes the other. If you wish to build objects as you go rather than read everything into memory, use SAX. If you are happy to read everything in and then process it, use XOM.
I spent far too much time trying to get the W3C DOM to work - IMO it is poorly defined with too many ways of doing some things and not enough for others. When XOM came it revolutionised my productivity.
The XOM community is very knowledgeable and focused and helpful.

Categories