Splitting nested XML with Camel Tokenizer language - java

I am trying to split following XML with Camel's XMLTokenizer language:
<units>
<unit type="menu">
<id>1</id>
<unit type="submenu">
<id>1</id>
</unit>
</unit>
<unit type="menu">
<id>2</id>
<unit type="submenu">
<id>1</id>
</unit>
</unit>
</units>
My splitter looks like this:
.split().tokenizeXML("unit").streaming()
and my problem is that it is producing splitted XML without end tag like this:
<unit type="menu">
<id>1</id>
<unit type="submenu">
<id>1</id>
</unit>
It would appear that with tokenizeXML() it is not possible to get this working since it will just scan for </unit> end tag. What would be preferred way to handle this case? Is there some other splitting method that would get me the result that I need? I would like to use streaming() so splitting with xpath() is not an option for me.

You can use camel-stax that allows to use the SAX api that supports streaming mode.
http://camel.apache.org/stax
You would need to define a POJO and the JAXB annotations that declares the binding.

Related

JSON to XML with type attribute in java

I'm rewriting C# application into java code.
There is REST API which return jsons.
I have to parse json to XML but C# library and Java doing it in difference ways.
How to keep type= attribute in java? I can't use JAXB annotations becouse there are too many objects in response and they might changing. XML.toString(jsonObject) doesn't work for me.
C# parsing is done in this way:
XDocument.load(JsonReaderWriterFactory.CreateJsonReader(Encoding.ASCII.GetBytes(jsonString), new XmlDictionaryReaderQuotas()));
C# result:
<root type="object">
<Items type="array">
<item type="object">
<Name type="string">test</Name>
<Total type="number">12.8000000</Total>
<CurrencyCode type="string">CHF</CurrencyCode>
<Country type="string">CH</Country>
</item>
</Items>
</root>
Java result:
<root>
<Items>
<item>
<Name>test</Name>
<Total>12.8000000</Total>
<CurrencyCode>CHF</CurrencyCode>
<Country>CH</Country>>
</item>
</Items>
</root>
I've used org.w3c.Document and org.w3c.dom.Element and set up attribute "type".
Anyway thanks for help :)

how to compare two xml files depending on a specific attribute value?

i want to know if it is possible to make xmlunit compare only nodes that have the same value of a specific attribute.. for example in make case i want xmlunit to compare nodes which have the same value of MOID ( attribute )
here is two xml files :
controle.xml:
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<HWData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Header xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" version="1.0"/>
<NE MOID="WBTS-4073" NEType="WBTS">
<EQHO MOID="EQHO-1-0" serialNumber="V7124400225">
<UNIT MOID="UNIT-FAN-1" State="enabled" serialNumber="V735"></UNIT>
<UNIT MOID="UNIT-FAN-3" State="enabled" serialNumber="V745"></UNIT>
<UNIT MOID="UNIT-FAN-2" State="enabled" serialNumber="V734"></UNIT>
</EQHO>
</NE>
</HWData>
test.xml :
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<HWData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Header xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" version="1.0"/>
<NE MOID="WBTS-4073" NEType="WBTS">
<EQHO MOID="EQHO-1-0" serialNumber="V7124400225">
<UNIT MOID="UNIT-FAN-2" State="enabled" serialNumber="V734"></UNIT>
<UNIT MOID="UNIT-FAN-1" State="disabled" serialNumber="V735"></UNIT>
<UNIT MOID="UNIT-FAN-3" State="enabled" serialNumber="V745"></UNIT>
</EQHO>
</NE>
</HWData>
Is there a way to achieve that with xmlunit !!
any other suggestions are welcomed..
thanks in advance

Mule iterate over xpath result

I've set the following xml as payload in order to iterate over every product using splitter component.
<root>
<product>
<id>1</id>
<name>apple</name>
</product>
<product>
<id>2</id>
<name>orange</name>
</product>
</root>
<splitter expression="#[xpath('//product')]" />
The splitter component returns an object of type org.dom4j.tree.DefaultElement on which I call the method asXML() to get single product's xml.
First iteration
<product>
<id>1</id>
<name>apple</name>
</product>
Second iteration
<product>
<id>2</id>
<name>orange</name>
</product>
I need to replace splitter with foreach component, but I'm having some troubles.
<foreach collection="#[xpath('//product')]">
...
</foreach>
The foreach component returns an object of type org.apache.xerces.dom.ElementNSImpl which hasn't the method asXML().
Any idea how I can get products'xml as String as explained in the first example?
Thanks in advice!
Use Mule's dom-to-xml-transformer.
Reference: http://www.mulesoft.org/documentation/display/current/DomToXml+Transformer

How can i parse the following XML using JDOM

I have an XML document as follows:
<?xml version="1.0" encoding="UTF-8"?>
<decision>
<question id="0">
<questionText>What type is your OS?</questionText>
<answer id="0">
<answerText>windows</answerText>
</answer>
<answer id="1">
<answerText>linux</answerText>
</answer>
<answer id="2">
<answerText>mac</answerText>
</answer>
</question>
<question id="1">
<questionText>What are you looking for?</questionText>
<answer id="0">
<answerText>table</answerText>
<question id="0">
<questionText>Which color table you want?</questionText>
<answer id="0">
<answerText>green</answerText>
</answer>
<answer id="1">
<answerText>black</answerText>
</answer>
<answer id="2">
<answerText>pink</answerText>
</answer>
</question>
</answer>
<answer id="1">
<answerText>chair</answerText>
</answer>
<answer id="2">
<answerText>bed</answerText>
</answer>
<answer id="3">
<answerText>cloth</answerText>
</answer>
</question>
Now I want to parse the above XML using jdom in Java. It kind of recursive and important thing to note is a Question can't be a direct child of Question and same applies for Answer.
Article
In the light of previous related questions, I'd like to repeat and stress the advice of others (like JB Nizet commented on this question):
Learn Java, learn XML, pick the tools and API's you need for your project and learn to use those too. If at one point you get into trouble, everybody here will be happy to help you out debugging your code.
I'm aware that this may seem harsh but it gets to the point where your program gets built by StackOverflow users and not yourself.
That being said, the link at the top of this answer leads to a tutorial on using JDOM to traverse your XML.
Use Element.getChildren(String) to get all of the question tags and loop through that List - calling getChildren(String) to get all of the answers, or getChild(String) if there can be only one child element.
first that you need use is XSD to validate the XML.

Mapping collection to XML in Castor

I'm trying to map a POJO to XML using Castor.
Let's say I have a Order that has a collection of Items... is there any way of achieving an xml like the following:
<order>
...order attributes
<items>
<item> ..item attributes </item>
<item> ..other item </item>
</items>
</order>
I could make something similar but without the <items> node. This wouldn't be a problem in other case but my XML must adhere to a strict XSD schema so I need to do it like that.
Thanks!
I though of a kind of "workaround" that would involve creating a new java object (that would be the node) that would only contain the list of items... can anyone think of a better approach? there's a 100 rep bounty open since now!
You can use the location attribute of the bind-xml lement
http://castor.codehaus.org/1.2/xml-mapping.html#6.-Location-attribute
Example from the docs:
<class name="Foo">
<field name="bar" type="Bar">
<bind-xml name="bar" location="abc"/>
</field>
</class>
Produces the following XML:
<foo>;
<abc>
<bar>...</bar>
</abc>
</foo>
The other answer doesn't use the collection attribute which I think is probably what you're ultimately needing.
Something like this might work when included in your mapping for the Order object:
<field name="items" type="item" collection="arraylist" >
<bind-xml name="items" node="element"/>
</field>

Categories