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.
Related
My Project Manager told me to move all the queries in a xml file (he even made for me), so when the user (via jsp) select the description: "Flusso VLT mensile" he has 2 options, click search, update or download, (the download it works now but I need to get the name of filename), he told me to work with jaxb but I don't think is necessary
<flow-monitor>
<menu1>
<item id="7" type="simple">
<connection name="VALSAP" />
<description value="Flusso VLT mensile" />
<filename value="flussoVltmensile" />
<select><![CDATA[
SELECT * FROM vlt_sap WHERE stato=7
]]>
</select>
<update>
<![CDATA[update vlt_sap set stato = 0 where stato =7]]>
</update>
</item>
<item id="11" type="simple">
<connection name="VALSAP" />
<description value="Flusso REPNORM BERSANI" />
<filename value="flussoRepnormBersani" />
<select><![CDATA[
select * from repnorm_bersani_sap where stato = 99
]]>
</select>
<update>
<![CDATA[update repnorm_bersani_sap set stato=0 where stato = 99]]>
</update>
</item>
</menu1>
</flow-monitor>
On java I should read this xml and depending on <description value=> I should execute the query inside them, any way to easily read the value inside without make a lot of if statement
Anybody knows a good and easy way to achieve all this?
Thanks
There are a few ways to read the XML file and extract the information you need without using a lot of if statements. One approach is to use an XML parsing library such as JAXB or SAX, and create Java classes to represent the XML elements.
In JAXB, you can use the javax.xml.bind.Unmarshaller class to unmarshal the XML file into a Java object, which you can then traverse to extract the information you need.
You should start creating a Java classes based on the XML structure, like FlowMonitor, Menu1, Item, Connection etc. , and use annotation to map the xml elements to the fields.
Then, you can use the unmarshaller.unmarshal() method to parse the XML file and create an instance of the FlowMonitor class, which will contain all the information from the XML file.
Once you have the FlowMonitor object you can loop through the items, and get the description and filename by calling getDescriptionValue() and getFilenameValue() of the item object....
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.
<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
I want to parse like this xml file in java.
I know using SAX or DOM, we can parse XML file.
But as per my knowledge if xml is like this
<XML><FORM><ITEM>Name</ITEM> <ITEM>Area</ITEM> <ITEM>ZipCode</ITEM> </FORM></XML>
we can parse it.
How can i get other properties like label, title,type. as in this XML file.
How to do that?
Please help me!!!
<XML><FORM TITLE="Search" View="1"><ITEM Label="Name" Type="Alpha" maxWidth="25"ID="name" Align="LEFT"></ITEM> <ITEM Label="Area " Type="AlphaNumeric" maxWidth="20" ID="area" Align="LEFT"></ITEM> <ITEM Label="Zip Code" Type="Numeric" maxWidth="10" ID="zip" Align="LEFT"></ITEM><ITEM Label="Search within radius of" Type="Radio" maxWidth="20" ID="ID" Align=" CENTER"><LIST_VALUES><ID="20" VALUE="20 kms"<ID="50" VALUE="50 kms"> <ID="100" VALUE="20 kms"></LIST_VALUES></ITEM></FORM><XML>
TITLE, Label, etc are ATTRIBUTES of the various XML Nodes. Depending on the exact api you're using you'd generally find a method named something like getAttribute(String name) which would allow you to retrieve the content of the particular attribute.
I remember JDOM allowing you to call getAttribute(..) on an instance of an Element, which mapped to an XML node
Here is an exemple using DOM, once you have your ITEM element (for instance):
String labelValue= itemElement.getAttribute("Label");
In my app I am parsing 1 main and number of supporting xml files. The tags of my main xml files are as below:
<subject>
<chapter>
<chaptername>A</chaptername>
<pack>
<packname>A1</packname>
<packname>A2</packname>
</pack>
</chapter>
<chapter>
<chaptername>C</chaptername>
<pack>
<packname>C1</packname>
<packname>C1</packname>
</pack>
</chapter>
</subject>
When the first xml file is parsed, it lists the chapter names, and if I click any of the chapter names, it lists the pack names.
Now when I click on the pack name, I want the corresponding supporting xml file, which I have stored in the raw folder, to be parsed. How do I do this?
Look at QueryPath for parsing XML in PHP. After you parse the information you can either pull in other XML files or XML strings from a database based on what the clicked tag is. Use jQuery for the AJAX part of it.
Sources:
querypath.org
jquery.org