So todays subject is XML parsing.
Right, you'll say this is a common thing, who didn't work with XMLs.
And you are right, but, as it turns out my line of work didn't usually take me to work with xml files.
Thing is I needed to take an XML string and just add it a new node.
This example it's quite simple except in my case I needed to use name spaces, for the xml file used them.
So with out further due here it is:
SAXBuilder builder = new SAXBuilder();
Document document;
document = builder.build(new ByteArrayInputStream(xmlMessage.getBytes()));
Namespace atomNS = Namespace.getNamespace("atom", "http://www.w3.org/2005/Atom");
Namespace cmisraNS = Namespace.getNamespace("cmisra", "http://docs.oasis-open.org/ns/cmis/restatom/200908/");
Namespace cmisNS = Namespace.getNamespace("cmis", "http://docs.oasis-open.org/ns/cmis/core/200908/");
Element newElement = new Element("propertyString").setAttribute("propertyDefinitionId", "aNameSpace:uuid");
newElement.setNamespace(cmisNS);
Element newValue = new Element("value").setNamespace(cmisNS).addContent(uuid);
newElement.addContent(newValue);
Element rootElement = document.getRootElement();
rootElement.getChild("object", cmisraNS).getChild("properties", cmisNS).addContent(newElement);
As you can see there is no much new here, even more if you compare it to this site that I used as a starting point.
The only difference is that as my XML has namespaces you must use them in order to JDom parse the document correctly. This is, to get the actual node you are looking for.
In the same way when you create your new elements/nodes you must specify the name space that should be used.
Really man!, http://code.google.com/p/syntaxhighlighter/
ResponderEliminar