Search query in XML file from java - java

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....

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 read all the tags present inside entry tag based on tag name and store its value in a hashmap using java

Here is the xml code in which I want to get value based on tag name which are present under entry tag and store it in hashmap as a key value pair
<entry xmlns="http://www.w3.org/2005/Atom">
<title>Isra쭩쳳 doden drie Palestijnse aanvallers</title>
<updated>2016-02-17T17:13:11.477Z</updated>
<rights>This material may not be published</rights>
<content type="text/plain" xml:lang="nl">Isra쭩쳳 doden drie Palestijnse aanvallers</content>
<apcm:ContentMetadata xmlns:apcm="http://ap.org/schemas/03/2005/apcm">
<apcm:Priority Numeric="4" Legacy="r" />
<apcm:ConsumerReady>TRUE</apcm:ConsumerReady>
<apcm:ByLine>Paul Gabel</apcm:ByLine>
<apcm:HeadLine>Isra쭩쳳 doden drie Palestijnse aanvallers</apcm:HeadLine>
<apcm:OriginalHeadLine>Isra쭩쳳 doden drie Palestijnse aanvallers</apcm:OriginalHeadLine>
<apcm:Keywords>Westoever</apcm:Keywords>
<apcm:Cycle>BC</apcm:Cycle>
<apcm:Selector>-----</apcm:Selector>
<apcm:SlugLine>Westoever 2016/02/17 22:43:58</apcm:SlugLine>
<apcm:MediaType>Text</apcm:MediaType>
<apcm:SubjectClassification System="Editorial" Authority="AP Category Code" Value="DUT" Id="DUT" />
<apcm:Property Name="EntitlementMatch" Id="urn:publicid:ap.org:product:45147" Value="All Text for Elasticsearch" />
<apcm:Property Name="SequenceNumber" Value="InSequence" Id="236167493" />
<apcm:FirstCreated>2016-02-14T15:43:48Z</apcm:FirstCreated>
</apcm:ContentMetadata>
<apnm:NewsManagement xmlns:apnm="http://ap.org/schemas/03/2005/apnm">
<apnm:ManagementId>urn:publicid:ap.org:f02232ade7f4394d95337af4b9200c47</apnm:ManagementId>
<apnm:ManagementSequenceNumber>0</apnm:ManagementSequenceNumber>
</apnm:NewsManagement>
</entry>
Use JaxB. Java Architecture for XML Binding (JAXB) provides a fast and convenient way to bind XML schemas and Java representations, making it easy for Java developers to incorporate XML data and processing functions in Java applications.
example

Xml id attribute to work with Java's getElementById? [duplicate]

This question already has answers here:
Java DOM getElementByID
(2 answers)
Closed 3 years ago.
I have an xml document being parsed in Java as a w3c document.
In my xml, i have many elements of the same name, e.g <item ..... />, each one with unique attribute's value, e.g <item name="a" .... />.
I want in java to do:
doc.getElementById("a")
in order to get that specific item I have there with that name.
How can I tell java to use 'name' as the id?
Or, alternately, How can I fetch that specific item in least complexity?
DOM is not the best API to easily query your document and get back found elements. Learn XPath, which is a more appropriate API, or iterate through the tree of elements by yourself.
getElementById() will only return the element which has the given id attribute (edit: marked as such in the document DTD or schema). It can't find by name attribute.
See Java XML DOM: how are id Attributes special? for details.
You need to write a DTD that defines your attribute as being of type ID.
Well, To make a complete answer, I had to use DTD schemas like everyone stated.
Since my needs are quite simple, I added it in embedded in my xml the following way:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [
<!ATTLIST item
name ID #REQUIRED
>
]>
<root> .... </root>
The only important thing left to know is that once you declare the ATTLIST, I have to declare all of the rest of my attributes, therefore, you need to add IMPLIED:
some-attribute CDATA #IMPLIED
It says that some-attribute contains some data (can use also PCDATA for parsed cdata), and is implied, which means, it can be there or it cannot. doesnt matter.
So eventually, it'll look something like:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [
<!ATTLIST item
name ID #REQUIRED
some-attribute CDATA #IMPLIED
>
]>
<root> .... </root>
And from Java side, Just use it blindly, e.g getElementById("some-name")
In order to make doc.getElementById("a") work you need to change your XML to <item id="a" name="a" .... />
If you can't change the XML, you could use XPath to retrieve this element.

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.

java,Xml parsing

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");

Categories