Spring Batch Xml Item Read - java

I'm working with Spring Batch.
I think I already know how I can parse good-looking xml and binding object.
And I stuck with this problem.
<parent>
<child>
<key name="xxx">
<value>val</value>
</key>
<key name="yyy">
<value>val</value>
</key>
</child>
</parent>
How can I parse xml items for above xml to children defined like this?
class Child {
private String xxx;
private String yyy;
}
I know how I can parse when it looks like this.
<parent>
<child>
<xxx>val</xxx>
<yyy>val</yyy>
</child>
</parent>
Is there any XPath like mappings with Spring-Batch's XML item reader?

Use a Jaxb2Unmarshaller.
Check aroudn for a jaxb solution: it's full around the net.
In the worst case I remember you can write your own node parser and manage this form of your child tag.
I'm sorry but I can't add code because I'm not at work.
Hope can be help

Related

How to get xml nodes count in camel

I want to get the count of xml nodes present in the file with specific tags in camel exchange or camel route.
My xml tags are like this:
<parent>
<child>
<data>A</data>
</child>
<child>
<data>B</data>
</child>
<child>
<data>C</data>
<child>
<data>C1</data>
</child>
<child>
<data>C2</data>
</child>
</child>
</parent>
I want to count the <child> tags and it should return 5 for this.
Currently, I am getting size using Exchange but it is giving output as 3.
exchange.getIn().getBody(XmlTreesType.class).getParentTree().getChildNode().size();
This highly depends on your actual use case.
To extract the count, you could use the XPath language which allows you to extract information from XML easily.
To extract the count of all <child> nodes within your <parent> you could use the following:
count(/parent//child)
XPath expression.
To extract this value and store it in a header variable would look like this:
.from()
.setHeader("childCountHeader", xpath("count(/parent//child)", Integer.class));
Another typical use case in the camel Java DSL would be along the following, in order to directly route based on the count:
from()
.choice().xpath("count(/parent//child)>5")
//do something
.otherwise()
//do something else
.end();
If you want to use XPath inside vanilla java, as in a camel processor. You can build up an XPath processor as described in this answer.

someone knows how to create the next CDATA xml structure in java?

i need create next structure en xml
<ser:myobjet soapenv: encodingstyle="http://schemas.xmlsoap.org/soap/encoding/">
<xml xsi:type='xsd.string'>
<!^[CDATA[<!xml version="1.0" encoding="iso-8859-1"?>
<object>
<service>
<request>
<idservice>65445</idservice>
<date>14-08-2016</date>
<additionalData>
<user>user</user>
<city>mycity</city>
</additionalData>
</request>
</service>
</object>]]>
</xml>
See I have a father and his children, yet one of them has their own children
<request>
<idservice>65445</idservice>
<date>14-08-2016</date>
<additionalData>
<user>user</user>
<city>mycity</city>
</additionalData>
</request>
I'm really lost and do not know how to create that structure.
Someone knows or some example of how to create it?
As I return a CDATA me and I need access to those tags, any idea how to do it?
Thank you.

Fetch value of a node's attribute in XML

I have an XML document, a snippet of which is below:
<item name="entryDataItem" type="dataView" caption="__entry_data_item" id="208" formItem="selectbox">
<properties>
<property name="caption" value="Task Type"/>
<property name="name" value="task_type"/>
</properties>
</item>
<item name="entryDataItem" type="dataView" caption="__entry_data_item" id="211" formItem="text">
<properties>
<property name="caption" value="Time Spent (Min)"/>
<property name="name" value="time_spent_min"/>
</properties>
</item>
etc.
There are other "properties" as well. The value of "task_type" as well as all other properties are stored in database. When I iterate through the document, all the properties are fetched and a document is prepared with "caption" and "value" from the XML. The problem is , all the "properties" are printed. But I want contents of the document to be based on the "task_type"; say when the task_type value is TESTING", then only task_type "ESTIMATION" would be printed and nothing else.
I understand I have to put a check just before where the document is being prepared.
My question is: How do I put the condition when I only have "task_type" in the XML and not a value(eg."TESTING") directly within the XML document?
My code is a simple JAVA code.
Any help as to how this can be achived is much appreciated.
In my opinion, XPath is the bee's knees when it comes to going after specific items in a XML document. This article might be of some use if it's something you want to persue.

XML parsing /JAVA

I have an XML, for example
<root>
<config x="xxx" y="yyyy" z="zzz" />
<properties>blah blah blah </properties>
<example>
<name>...</name>
<decr>...</descr>
</example>
<example>
<name>...</name>
<decr>...</descr>
</example>
</root>
and I need to get nodes config, and properties and all values in it.
Thank you
Xpath can fetch you the data in the config tag. You need to create an expression first like this
expression="//root/config/#x", to get value of x,y,z.
For properties, follow this thread :
Parsing XML with XPath in Java
Hope this helps
DOM,DOM4J,SAX..
if the size of XML file is small,you can use DOM or DOM4J,but the size is big , you use the SAX
If you directly want to query or fetch data XPath can help, but if you want the data as Java Objects so that you can use it further then use JAXB
You can use SAX parser to read the xml manipulate its event based parsing and consumes more memory.
If your xml is big and requires lot of manipulations then go-for DOM/DOM4j either is good. DOM4L is very latest. DOM is widely used in industry.
Based on your requirement go for good parser.
Thanks,
Pavan

JAXB Vs JDOM : is it possible to update xml file using JAXB

i have been used JDOM to perform xml data entry & updation in any XML file, but now i am trying to use JAXB instead of JDOM but getting some difficulties.
as i know marshalling & unmarshalling in jaxb but when it comes to entry a new data into a xml at specified location (node), i find difficulties. e.g. for new entry Japan where id = Asia
<file>
<parent>
<node id="Asia">
<name>India</name>
<name>China</name>
</node>
<node id="Europe">
<name>UK</name>
</node>
</parent>
</file>
is there anybody who has idea about it.
If I'm not mistaken JAXB and JDOM and completely different things. JAXB will serialize java objects into an XML format and vice versa. JDOM simply reads in the XML file and stores it in a DOM tree which can then be used to modify the xml itself.
Using JAXB in this way is like trying to at runtime add a new variable to a class. It cannot be done. (at least to my knowledge).

Categories

Resources