Parsing and retrieving values from XML using java - java

My Sample XML:
<Group-1>
<One>
<two>Name1</two>
<three>Type1</three>
</One>
<One>
<two>Name2</two>
<three>Type2</three>
</One>
</Group-1>
<Group-2>
<One-2></One-2>
</Group-2>
There are two XML different XML Parent tags in my XML.
How can i retrieve the values from the above XML using XML parsers using core java?
I have seen examples using getelementsbytagname("").But i need some method to retrieve all the values when there are many repeats and also for simple XML tags?
Can anyone plz help me on this?

you need to use JAXBContext marshall and unmarshall

Related

How to convert nested tags in dynamic XML into a for loop?

I want to convert dynamic xml file into a specific file format. i could able to parse the xml using jsoup parser but the problem is I want to parse the nested tags and put it into a for-loop.Is there any way to do it. Attaching the sample below for reference
Input XML(sample)
<lineComponents>
<invoiceComponents>
<invoiceComponent>
<type></type>
<name></name>
<amount>16.00</amount>
<taxPercentage>0.00</taxPercentage>
<taxAmount>0E-8</taxAmount>
</invoiceComponent>
</invoiceComponents>
<acctComponents>
<acctComponent>
<componentCode>BASE</componentCode>
<glAccountNr></glAccountNr>
<baseAmount>10.00000</baseAmount>
<taxRate>0.00</taxRate>
<taxAmount>0.00000</taxAmount>
<totalAmount>10.00000</totalAmount>
<isVAT>No</isVAT>
</acctComponent>
<acctComponent>
<componentCode></componentCode>
<glAccountNr></glAccountNr>
<baseAmount>3.00000</baseAmount>
<taxRate>0.00</taxRate>
<taxAmount>0.00000</taxAmount>
<totalAmount>3.00000</totalAmount>
<isVAT>No</isVAT>
</acctComponent>
<acctComponent>
<componentCode>DISC</componentCode>
<glAccountNr></glAccountNr>
<baseAmount>-2.00000</baseAmount>
<taxRate>0.00</taxRate>
<taxAmount>0.00000</taxAmount>
<totalAmount>-2.00000</totalAmount>
<isVAT>No</isVAT>
</acctComponent>
<acctComponent>
<componentCode>ARPIT</componentCode>
<glAccountNr></glAccountNr>
<baseAmount>5.00000</baseAmount>
<taxRate>0.00</taxRate>
<taxAmount>0.00000</taxAmount>
<totalAmount>5.00000</totalAmount>
<isVAT>No</isVAT>
</acctComponent>
</acctComponents>
</lineComponents>
Expected output:
for(OrderItem invoiceLineItem: orderLineWrp.invoiceLineItems){
Dom.XMLNode invoiceComponentNode = invoiceComponentsNode.addChildElement(EP_OrderConstant.invoiceComponent,null,null);
invoiceComponentNode.addChildElement(EP_OrderConstant.seqId,null,null).addTextNode(getValueforNode(invoiceLineItem.EP_SeqId__c));
invoiceComponentNode.addChildElement(EP_OrderConstant.TYPE,null,null).addTextNode(getValueforNode(invoiceLineItem.EP_ChargeType__c));
invoiceComponentNode.addChildElement(EP_OrderConstant.name,null,null).addTextNode(getValueforNode(invoiceLineItem.EP_Invoice_Name__c));
invoiceComponentNode.addChildElement(EP_OrderConstant.amount,null,null).addTextNode(getValueforNode(invoiceLineItem.UnitPrice)); //Value for amount
invoiceComponentNode.addChildElement(EP_OrderConstant.taxPercentage,null,null).addTextNode(getValueforNode(invoiceLineItem.EP_Tax_Percentage__c)); //Value for taxPercentage
invoiceComponentNode.addChildElement(EP_OrderConstant.taxAmount,null,null).addTextNode(getValueforNode(invoiceLineItem.EP_Tax_Amount_XML__c)); //Value for taxAmount
}
This Xml file is dynamic. Is there any way to handle dynamic XML file into a specific format like above?
Jsoup is rather for HTML parsing.
If you have XSD/DTD to your XML, you should use JAXB-generated classes and an unmarshaller to read it.
Otherwise you can use JAXP (DOMParser, if the file is small, and XPath, or event based SAXParser(however this is not so easy to use) for really large XML files).

Validate XML against a catalog of values in other xml

I have a XML file with the following content:
<example>
<firstNode>
<someInfo>Hello</someInfo>
</firstNode>
<secondNode>
<myFlagColors>
<using>RED</using>
<using>WHITE</using>
<using>BLUE</using>
</myFlagColors>
</secondNode>
</example>
I have to check that every node <using>XYZ</using> has a value (like XYZ) coming from another XML like this one:
<colorCatalog>
<color>WHITE</color>
<color>BLACK</color>
<color>RED</color>
<color>GREEN</color>
<color>BLUE</color>
<color>YELLOW</color>
<color>PINK</color>
<color>ORANGE</color>
<color>CYAN</color>
</colorCatalog>
I donĀ“t like my current implementation made with java:
Convert every XML in a java Bean (using jaxb) and then use an iterator
to check if the value in the first bean in in the array of values of the second object.
My question: Is it possible to do this just by using xsd files? or any other way simpler than pure programming?
XML Schema Approach
If you can combine the XML documents then you could create an XSD that has a keyRef relationship between the 2 elements.
Validating the document against the schema would then highlight any errors.
If you can't combine the XML files easily then you could use xinclude (as long as your XSD parser supports it). Xerces for example supports it.

Is it possible to validate XML against schemas present in that XML file using Java?

So I have an XML that already has all the schemas specified in the root tag like this
<Waybill xmlns="urn:some:uri1"
xmlns:cac="urn:some:uri2"
xmlns:cbc="urn:some:uri3"
xmlns:ext="urn:some:uri4" xmlns:xxx="http://some.url.com">
Is it possible to validate my XML against these in the Java code in such a way that I don't have to repeat them in the Java code again?
All the examples I found so far require you to specify the url explicitly in the code
Try this: Validating XML against XSD
The magic seems to be in this: XMLConstants.W3C_XML_SCHEMA_NS_URI.

How can I use XPath to create a new xml content?

I have the following problem. Into a Java application I have to create a new XML content using XPath (I always used it to parse XML files and obtain values inside its tag, can I use it also for build a new XML content?).
So my final result (that have to be saved on a database CLOB field, not on an .xml file, but I think that this is not important) have to be something like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<Messaggio>
<Intestazione>
<Da>06655971007</Da>
<A>01392380547</A>
<id>69934</id>
<idEnel/>
<DataInvio>2015-05-06</DataInvio>
<DataRicezione/>
<InRisposta/>
<TipoDoc>Ricevuta</TipoDoc>
</Intestazione>
<Documenti>
<Ricevuta>
<Testata>
<Documento>
<Tipo>380</Tipo>
<NumeroDocumento>ff</NumeroDocumento>
<Stato>KO</Stato>
<Data>2014-03-10</Data>
</Documento>
</Testata>
<Dettaglio>
<Messaggio>
<Codice>000</Codice>
<Descrizione>Documento NON Conforme / NON dovuto</Descrizione>
</Messaggio>
</Dettaglio>
</Ricevuta>
</Documenti>
</Messaggio>
So what I need to do is to programmatically add the nodes and the content of these nodes (the content is obtained from a model object).
Can I do it using XPath? How?
Tnx
XPath is an API to locate nodes in a XML document. It can't create new nodes or manipulate existing nodes. So what you need is to locate the nodes to modify using XPath and then use the API of the found nodes to make the changes.
But in your case, you're starting with an empty document. Have a look at frameworks like JDOM 2 to build XML documents from scratch. This tutorial should get you started: http://www.studytrails.com/java/xml/jdom2/java-xml-jdom2-example-usage.jsp
You can't. XPath is a matching technology, not a content creation technology. Possibly you are looking for XSLT?

How do I parse XML data with xmlbeans?

I want to parse xml data using xmlbeans and store it into objects,The following is the xml file, I want to parse it, I have model I just want to store all data into model objects,
<?xml version="1.0" encoding="UTF-8" ?>
<data:events>
<data:event>
<data:name>test</data:name>
<data:date>2013-05-20-04:00</data:date>
<data:track>
<data:name>test</data:name>
<data:race>
<data:raceNumber>1</data:raceNumber>
<data:postTimeDisplay>5:25 PM</data:postTimeDisplay>
<data:runner>
<data:programNumber>1</data:programNumber>
<data:ownerName>user</data:ownerName>
</data:runner>
<data:runner>
<data:programNumber>7</data:programNumber>
<data:ownerName>PP</data:ownerName>
</data:runner>
</data:race>
</data:track>
</data:event>
</data:events>
I tried and search lot for parsing the above data using xmlbeans but I didn't get proper solution for the same and I am quit new in xml data parsing,
Please have look this and suggest to parse this xml.
So you dont want to use SAX / DOM parsers ?
For xmlbeans did you get chance to look at
http://www.ibm.com/developerworks/library/x-beans1/
http://xmlbeans.apache.org/docs/2.0.0/guide/conGettingStartedwithXMLBeans.html
As you said you are new to parsing xml using java and If you want to use SAX/DOM go throug
http://www.youtube.com/watch?v=q-oQ5D91TAk

Categories

Resources