parse XML to POJO using XStream - java

I have an xml file which I want to read and parse it into POJO.
I am using XStream for this.
I am not able to send the file as an input to the code for parsing(file is on my local drive).
How to read the xml file and parse it using fromXML() method ?
I would be gratefull if someone can give example for sending xml file as input and parsing it to POJO and printing it on the screen
Thanks...

Based on the provided comments, I can better understand the problem you are facing. I believe the answer you are looking for is implicit collections. Check out the XStream tutorial on aliasing: http://x-stream.github.io/alias-tutorial.html .

Related

Read any file from DRL file Drools

Is there any way to read or parse any file format ex: JSON or XML from DRL file Drools ? for example:
when
SomeClass()
read any file type JSON or XML
then
Do something
No. You will need to read and parse the file as you want outside of the DRL and then feed the results of the parsing into your session.
You can also do the parsing in the RHS of a rule if you want.
Hope it helps,

Convert one json format to another in java

I am looking for a utility which converts one json format to another by respecting at the conversion definitions from a preferably xml file. Is there any library doing something like this in java ?
For example source json is:
{"name":"aa","surname":"bb","accounts":[{"accountid":10,"balance":100}]}
target json is :
{"owner":"aa-bb","accounts":[{"accountid":10,"balance":100}]}
sample config xml :
t.owner = s.name.concat("-").concat(surname)
t.accounts = t.accounts
Ps:Please dont post solutions for this example, it is just for giving an idea, there will be quite different scenarios in mapping.
Is this what u need?
Open input file.
Read / parse JSON from file using a JSON library.
Convert in-memory data structure to new structure.
Open output file
Unparse in-memory data structure to file using JSON library.

xml to json transformation Spring/Java

I am looking for "transforming" (not plain-converting) XML(one data format) to JSON (to other/diff data format). Is there any tool/lib/framework to achieve this in Spring or Java?
Example
Input:
<root>
<element1>abc</element1>
<element2>xyz</element2>
.....
</root>
Output
{
"regionCode":"abc",
"regionName":"xyz"
...............
}
Thanks
Bharath
Please consider searching for your question. Have you considered camel? http://camel.apache.org/xmljson.html
What is the difference between "transform" and "convert"? Regardless, Jackson can read/write both JSON and XML (using the XML extension). You'll have to deserialize your XML into objects before serializing them as JSON but that's not a bad thing - it would be difficult to go directly from XML to JSON except in the most simple cases (JSON does not have attributes, for example).

How to read an XML using Xmlreader in java?

I have an XML file and want to read the information using XPath, I want to read the 'listings_Id' and 'budget_remaining' together.
XML example
<ads>
<ad>
<listing_ids>
<listing_id>2235</listing_id>
<listing_id>303</listing_id>
<listing_id>394</listing_id>
</listing_ids>
<reference_id>11</reference_id>
<net_ppe>0.55</net_ppe>
<budget_remaining>50000.0</budget_remaining>
</ad>
<ad>
<listing_ids>
<listing_id>2896</listing_id>
</listing_ids>
<reference_id>8</reference_id>
<net_ppe>1.5</net_ppe>
<budget_remaining>1.3933399</budget_remaining>
</ad>
</ads>
I want to output it to a CSV file as the following
ListingId,BudgetRemaining
2235,0.55
303,0.55
394,0.5
2896,1.5
I am trying to use the example as
DataReader reader = new XmlReader(new File("links.xml"))
.addField("ListingId", "//ad/listing_ids/listing_id")
.addField("BudgetRemaining", "//ad/budget_remaining")
.addRecordBreak("//ad")
.setExpandDuplicateFields(true);
But it seems so I cannot find the jar file for XMLReader and DataReader and also I am going definitely wrong with the format. New to Java, please any help is appreciated.
You're following a tutorial for a commercial library ("Data Pipeline"), which is not shipped with the JDK and needs to be installed separately. Get if from the Download page and install it using their Getting Started manual.
Now, the classes should be found.

Xstream append to existing XML file

First let me say I am very new to Java. I've been trying to figure out how to append a chunk of XML to an existing XML file using Xstream.
Example XML:
<root>
<first>
<a>Some Value</a>
<b>Some Value B</b>
</first
<second>
<a>Another Value</a>
<b>Another Value B</b>
</second>
</root>
How would I go about appending the following using Xstream?
<third>
<a>More A</a>
<b>More B</b>
</third>
Have you followed the two minute tutorial of Xstream yet? This can be found here.
You should address some implementation choices first: which way to go with Xstream?
For example: is the XML document a large document or a small document (if small, you could use a DomDriver.If large, use a StaxDriver)?
Does your XML document uses namespaces? If so, be aware that not all Xstream parsers implement namespace awareness, see the Xstream faq.
More information can be found here to get you started.
Please provide us your SSCCE so that users can try out your code example.
See details on how to write a SSCCE here and here
Also include a small valid XML file in your SSCCE.

Categories