4

I'm trying to figure out how best to translate this:

<Source><properties>
  ....
  <name>wer</name>
  <delay>
    <type>Deterministic</type>
    <parameters length="1">
      <param value="78" type="Time"/>
    </parameters>
  </delay>
  <batchSize>
    <type>Cauchy</type>
    <parameters length="2">
      <param value="23" type="Alpha"/>
  <param value="7878" type="Beta"/>
    </parameters>
  </batchSize>
 ...
</properties></Source>

Into:

<Source><properties>
  ....
  <name>wer</name>
  <delay>
    <Deterministic Time="78"/>
  </delay>
  <batchSize>
      <Cauchy Alpha="23" Beta="7878"/>
  </batchSize>
 ........
</properties></Source>

I've tried using DocumentBuilderFactory, but I while I can access the value of the name tag, I cannot access the values in the delay/batch section. This is code I used

Element prop = (Element)propertyNode;

NodeList nodeIDProperties = prop.getElementsByTagName("name");
Element nameElement = (Element)nodeIDProperties.item(0);

NodeList textFNList = nameElement.getChildNodes();
String nodeNameValue = ((org.w3c.dom.Node)textFNList.item(0)).getNodeValue().trim();

//--------
NodeList delayNode = prop.getElementsByTagName("delay");

Calling getElementByName("type") or "parameters" doesn't seem to return anything I can work with. Am I missing something, or is there a cleaner way to process the exisiting xml.

The need to be in the defined format to allow for marshalling and unmarshalling by Castor.

Any help would be much appreciated.

3
  • 4
    This is more of XML transformation, shouldn't you be using XSLT?
    – saugata
    Mar 4, 2010 at 7:26
  • 1
    I would strongly recommend using XPATH to do any XML parsing, it is much more logical in my opinion. Here is an excellent tutorial (ibm.com/developerworks/library/x-javaxpathapi.html). Could you also elaborate on what exactly you get when you call getElementsByName("type")? NullPointerExceptions / Empty strings?
    – Luhar
    Mar 4, 2010 at 7:31
  • Your title is incorrect, you are not parsing but manipulating a DOM tree. Mar 4, 2010 at 8:28

4 Answers 4

6

There are a variety of ways to convert the XML.

1) You can use XSLT (XSL Transformations) to transform the XML. It is a XML based language used to transform XML documents in other XML documents, text, or HTML. The syntax is hard to learn. However it is a powerful tool for XML conversion. Here is a tutorial. For using XSLT with Java I would recommend Saxon which also comes with a nice documentation. The big plus using XSLT is that the conversion can be externalized in a seperate template. So your Java code is not obfuscated by the translation stuff. However, as mentioned the learning curve is definitly steeper.

2) You can use XPath to select the nodes easily. XPath is a query language for selecting nodes in a XML document. XPath is also used in XSLT by the way. E.g. the XPath query

//delay[type = 'Deterministic']/parameters/param/@value

selects all parameters value which are contained in a node param which are a child of delay containing a node type with the value "Deterministic". Here is a nice web application for testing XPath queries. Here is a tutorial how to use XPath in Java and here about XPath in general. You can use XPath expressions to select the right nodes in your Java code. IMHO this is far more readable and maintainable than using the DOM object model directly (which is also from time to time ackward as you have already learned).

3) You can use Smooks for doing XML transformations. This is especially useful if the transformation gets rather complex. Smooks populates a object model from the input XML and outputs the result XML via a templating mechanism either using Freemarker or XSL templates. Smooks has a very high througput and is used in high performance environments like ESBs (e.g. JBoss ESB, Apache ServiceMix). Might be overpowered for yur scenario though.

4) You could use Freemarker to do the transformation. I have no experience in this, but as I heared it can be used fairly simple. See the "Declarative XML processing" section of the documentation (also take a look at "Exposing XML documents" to learn how to read the source XML). Seems fairly simple to me. If you try your luck with this approach, I would love to hear about it.

3
  • You summarised what the rest of guys above have said. Thanks. I'll look into XSLT and if the curve is too steep (on a bit of time crunch) then I'll use the XPath query. Thanks. Mar 4, 2010 at 7:57
  • I saw that. So I have added some more resources. Freemarker seems to be really useful in your scenario...
    – spa
    Mar 4, 2010 at 8:08
  • XSLT is not bad if you are familiar with LISP-y languages. Mar 4, 2010 at 8:29
1

This looks like a job for XPATH or some other XML transformation API.

Check out: http://www.ibm.com/developerworks/library/x-javaxpathapi.html

0

Although probably XSLT is the best way to do this, if you want to use a JVM programming language and you want to learn a different approach, you can try scala's xml transformation library.

Some blog posts:

http://scala.sygneca.com/code/xml-pattern-matching

http://debasishg.blogspot.com/2006/08/xml-integration-in-java-and-scala.html

0

XSLT was the way forward in the end. Its actually pretty easy to use and the w3schools example is a good place to start.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.