convert xml to excel with specific xml element using java - java

<ns2:CategoryResponse xmlns:ns2="http://schemas.google.com/definitions/ps/v3" currentpage="0" querydate="2013-09-17T02:33:08.080-04:00" resptime="69ms" resultsperpage="0" totalresults="1">
<category id="65">
<product id="12304"/>
<product id="462289"/>
<product id="340287"/>
<product id="378825"/>
<product id="366310"/>
</category>
</ns2:CategoryResponse>
In the above xml context[sample.xml], I want to store the product ids from the sample.xml into Excel file using java. Please some on help on this.

you can take a look to this tutorial, it's little big long, but really great explanation.
xml to excel and viceVersa

Related

How do I parse an XML file in Java using either DOM or JAXB when file only has attributes

I apologize in advance for my ignorance but am desperate for help.
I am writing a java program that needs to read the following XML file snippets with either DOM or JAXB and will group all attributes in each
<?xml version="1.0" encoding="UTF-8"?>
<obj id="A" text="helpme1" name="button1">
<text text="save1" priority="yellow"/>
<app node="first" desc="newnode1"/>
</obj>
<obj id="B" text="helpme2" name="button2">
<text text="save2" priority="red"/>
<app node="second" desc="newnode2"/>
</obj>
Ultimate need:To be able to create an object for helpme1 and helpme2
that contains objects called text and app that go with each <obj. How does one parse this XML to get the <obj with text="helpme1" and the <text and <app below it and then get the <obj with text="helpme2 and the <text and <app
below that? Ultimately as I loop throw this file, I will have a map
that will set helpme1 and helpme2 as keys and the values will
be objects that will consist of <text and <app that are associated
with each <obj in the file. I am familiar with both JAXB and DOM. Those are my 2 options. I do not have a Schema.
Any suggestion are appreciated and I am well aware that I am ignorant
so please go easy. Thank you.

How to use xpath in camel when the outermost element has an xmlns attribute?

I am having some trouble using xpath to extract the "Payload" values below using apache-camel. I use the below xpath in my route for both of the example xml, the first example xml returns SomeElement and SomeOtherElement as expected, but the second xml seems unable to parse the xml at all.
xpath("//Payload/*")
This example xml parses just fine.
<Message>
<Payload>
<SomeElement />
<SomeOtherElement />
</Payload>
</Message>
This example xml does not parse.
<Message xmlns="http://www.fake.com/Message/1">
<Payload>
<SomeElement />
<SomeOtherElement />
</Payload>
</Message>
I found a similar question about xml and xpath, but it deals with C# and is not a camel solution.
Any idea how to solve this using apache-camel?
Your 2nd example xml, specifies a default namespace: xmlns="http://www.fake.com/Message/1" and so your xpath expression will not match, as it specifies no namespace.
See http://camel.apache.org/xpath.html#XPath-Namespaces on how to specify a namespace.
You would need something like
Namespaces ns = new Namespaces("fk", "http://www.fake.com/Message/1");
xpath("//fk:Payload/*", ns)
I'm not familiar with Apache-Camel, this was just a result of some quick googling.
An alternative maybe to just change your xPath to something like
xpath("//*[local-name()='Payload']/*)
Good luck.

Getting doc tag names while iterating the elements

i have this XML document:
<?xml version="1.0" encoding="UTF-16"?>
<root>
<items>
<item1>
<tag1>1</tag1>
<tag2>2</tag2>
<tag3>3</tag3>
</item1>
<item2>
<tag1>4</tag1>
<tag2>5</tag2>
<tag3>6</tag3>
</item2>
</items>
</root>
I want to iterate the item elements (item1, item2...), and for each tag get the tag name and after that the value of the tag.
I am using DOM parser.
Any ideas?
Sorry, but this ain't an unsolvable or complicated problem, this is simply reading a tutorial which can be googled within seconds.
And of course, you might also check the documentation, which will give you a hint about this handy method called "getNodeName()".

How to get data with tag name & their values inside parent tag in xml

I am working on Java. I am parsing an xml file, I am getting tag values, it is working. I have xml file as follows:
<DOC>
<STUDENT>
<ID>1</ID>
<NAME>DAN</NAME>
<ADDRESS>U.K</ADDRESS>
</STUDENT>
<STUDENT>
<ID>2</ID>
<NAME>JACK</NAME>
<ADDRESS>U.S</ADDRESS>
</STUDENT>
</DOC>
I have question that I want to fetch data inside <DOC>....</DOC> with their tag name & value as well. Means I want data as follows:
"<STUDENT>
<ID>1</ID>
<NAME>DAN</NAME>
<ADDRESS>U.K</ADDRESS>
</STUDENT>
<STUDENT>
<ID>2</ID>
<NAME>JACK</NAME>
<ADDRESS>U.S</ADDRESS>
</STUDENT>"
Please guide me how to do it.
The most common approaches in Java are to use one of either SAX or Dom parsing libraries.
If you look them up you should find loads of documentation/tutorials about them.
Dom is the easiest to use normally as it stores the entire XML in memory and you cna then access any tag, however, this is less performant and can be problematic if you are using very large XML. SAX requires more work, but reads the XML and processes each tag as it gets to it.
Both are able to do what you need though.
Take a look at SAX Parser.
This link might be helpful too: http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/

XML to XLSX and vice versa using java

I have to complete this task: I'm given a xml file which is exported from an application, I need to make an xlsx file out of it containing table hierarchy make changes to the xlsx file using excel then make it back to xml having all these changes. All this using Java. I'm aware of apache poi library. For xlsx you have to use XSSF instead of HSSF. I checked out their examples manipulating excel but I'm struggling around one point. Examples in the official poi websites are showing how to create xlsx file containing tables, cells containing various data but where goes the XML part and reading of XML file? I need xlsx file to look according to XML.
Any help or pointers into right direction would be greatly appreciated.
My XML example file:
<?xml version="1.0" encoding="utf-8" ?>
<data type="data">
<title>Duomenų suvedimo formos pavadinimas</title>
<fields>
<item id="org" label="Institucija/Įstaiga" valueField="orgId" labelField="orgLabel" tooltipField="orgTooltip" type="dim" />
<item id="R1" label="R1 pavadinimas" group="Katalogo pavadinimas" valueField="value1" type="value" inputEnabled="1" valueType="numeric"/>
<item id="R2" label="R2 pavadinimas" group="Katalogo pavadinimas" valueField="value2" type="value" inputEnabled="1" valueType="numeric"/>
<item id="R3" label="R3 išvestinis" group="Katalogo pavadinimas" valueField="value3" type="value" inputEnabled="0" valueType="numeric" formula="[R1]/12 + [R2]"/>
</fields>
<dataItems>
<item orgId="ins1" orgLabel="Institucija 1" orgTooltip="Institucija 1" grp="2" value1="10" value2="20" value3="" isval="1" edit="1" />
<item orgId="ins2" orgLabel="Institucija 2" orgTooltip="Institucija 2" grp="3" value1="15" value2="25" value3="" isval="1" edit="1" />
</dataItems>
</data>
There are two components to your problem -
1) XML parsing and XML generation and
2) forming XLSX out of the parsed XML data.
You are right about the second part to use Apache POI. For the first part, you would need an XML parser and should plug it in before feeding data to XLSX generation program - try checking out JAXB or a simple parser example http://www.java-samples.com/showtutorial.php?tutorialid=152.
Post any specific issues. An XML sample of what you are trying to parse may help.

Categories