How can I synchronize data between XML Files and SQLite? - java

I have a XML File which I read data from XML File into Java Object and then insert these Java Objects into SQLite Database.
Then I modify my XML File and insert more data into it, how can I keep data between XML File and SQLite synchronized.
My XML File
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Employees xmlns="https://www.journaldev.com/employee">
<Employee id="1">
<name>Pankaj</name>
<age>29</age>
<role>Java Developer</role>
<gender>Male</gender>
</Employee>
<Employee id="2">
<name>Lisa</name>
<age>35</age>
<role>Manager</role>
<gender>Female</gender>
</Employee>
</Employees>
Code to read XML File Employee in Java
https://anotepad.com/notes/qnn7nb2k

What you are looking for is a file watcher and since jdk1.7, you have the possibility to do this via WatchService.
A working example can be found in the docs here

Related

How can an xml string containing an embedded CDATA xml string be formatted as 'pretty' xml

I have an xml string containing an embedded CDATA xml string. I need to format 'pretty' xml
Example string:
<?xml version="1.0" encoding-\"UTF-8\" standlaone=\"no\"?>
<catalog>
<book id=\"b1\">
<title>XML Developer's Guide</title>
<description>An in-depth look at creating applications with XML.</description>
<data>
<![CDATA[<?xml version=\"1.0\" encoding-\"UTF-8\" standlaone=\"no\"?><details><author>Gambardella, Matthew</author><genre>Computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date></details>]]>
</data>
</book>
</catalog>
What is the easiest way in java or reactjs to create a pretty string:
<?xml version="1.0" encoding-\"UTF-8\" standlaone=\"no\"?>
<catalog>
<book id=\"b1\">
<title>XML Developer's Guide</title>
<description>An in-depth look at creating applications with XML.</description>
<data>
<?xml version=\"1.0\" encoding-\"UTF-8\" standlaone=\"no\"?>
<details>
<author>Gambardella, Matthew</author>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
</details>
</data>
</book>
</catalog>
I feel this cannot be done in reactjs and I would need to use java to extract the inner xml and create 2 xml:
<?xml version="1.0" encoding-\"UTF-8\" standlaone=\"no\"?>
<catalog>
<book id=\"b1\">
<title>XML Developer's Guide</title>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
<?xml version="1.0" encoding-\"UTF-8\" standlaone=\"yes\"?>
<data>
<details>
<author>Gambardella, Matthew</author>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
</details>
</data>
I would appreciate any ideas or alternatives using reactjs or java?
Obviously the XML parser doesn't know that the CDATA section contains XML (CDATA means "character data", that is it's explicitly telling the parser to treat the content as plain text, not as markup). So the only way to handle this is to put it through two stages of parsing: the second stage reads the text node representing the CDATA section, and parses it again, grafting the resulting node tree into the original tree.
Note that the output you want isn't well-formed XML: you can't have an XML declaration in the middle of the document. With that qualification, you can achieve the desired effect in XSLT 3.0 as:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="data">
<xsl:copy-of select="parse-xml(.)"/>
</xsl:template>
</xsl:transform>
which you can run from Java with the help of the Saxon library. (Disclaimer: my company's product.) The free open-source version will do the job.

How do i parse an XML file with multiple node in JAVA using JAXB?

I have and xml from a webservice but even if the structure is standard i have some problem parsing this file with JAVA Spring using JAXB, The structure it's something like this:
<organisation>
<organisation>
<organisationIdentification>
<uid>
<uidOrganisationIdCategorie>123</uidOrganisationIdCategorie>
<uidOrganisationId>1234531234</uidOrganisationId>
</uid>
<OtherOrganisationId>
<organisationIdCategory></organisationIdCategory>
<organisationId></organisationId>
</OtherOrganisationId>
<OtherOrganisationId >
<organisationIdCategory></organisationIdCategory>
<organisationId></organisationId>
</OtherOrganisationId>
<organisationName>Orgnanisation name</organisationName>
<organisationLegalName">Name SA</organisationLegalName>
<legalForm >3698</legalForm>
</organisationIdentification>
<nogaCode>123456</nogaCode>
<contact>
<address>
<otherAddressCategory>main</otherAddressCategory>
<postalAddress>
<organisation >
<organisationName>Name SA</organisationName>
</organisation>
<addressInformation>
<street/>
<town></town>
<zipCode></zipCode>
<country>
<countryIdISO2></countryIdISO2>
<countryNameShort></countryNameShort>
</country>
</addressInformation>
</postalAddress>
</address>
<address>
<otherAddressCategory></otherAddressCategory>
<postalAddress>
<organisation>
<organisationName>-</organisationName>
</organisation>
<addressInformation>
<street></street>
<town></town>
<zipCode></zipCode>
<country>
<countryIdISO2></countryIdISO2>
<countryNameShort></countryNameShort>
</country>
</addressInformation>
</postalAddress>
</address>
<address>
<otherAddressCategory>postbox</otherAddressCategory>
<postalAddress>
<organisation>
<organisationName>-</organisationName>
</organisation>
<addressInformation>
<street/>
<postOfficeBoxNumber></postOfficeBoxNumber>
<postOfficeBoxText/>
<town/>
<zipCode></zipCode>
<country>
<countryIdISO2></countryIdISO2>
<countryNameShort></countryNameShort>
</country>
</addressInformation>
</postalAddress>
</address>
<email>
<emailAddress>info#mail.ch</emailAddress>
</email>
<internet>
<internetAddress>http://www.site.ch</internetAddress>
</internet>
</contact>
<languageOfCorrespondance>IT</languageOfCorrespondance>
</organisation>
<uidregInformation>
<uidregSource>
<uidOrganisationIdCategorie></uidOrganisationIdCategorie>
<uidOrganisationId >11111111</uidOrganisationId>
</uidregSource>
<uidregSource>
<uidOrganisationIdCategorie>CHE</uidOrganisationIdCategorie>
<uidOrganisationId>23232111</uidOrganisationId>
</uidregSource>
<uidregSource>
</uidregInformation>
</organisation>
It's possible with JAXB parsing this file into a class model and keep all the value from the multiple nodes (uidOrganisationId / town / zipCode).
I don't know how to store the values ​​where there are same nodes.

How to count the elements in XML through DOM parser

I am trying to parse an XML using DOM parser. I would like to get the count of the records in the XML basing on the type "Senior Software Developer". Below is my XML file. Could someone suggest me how do I get the count.
<class>
<employee type="Senior Software Developer">
<empid>A433568</empid>
<empname>John Mathews</empname>
<address>6th Avenue Street</address>
</employee>
<employee type="Junior Software Developer">
<empid>A433678</empid>
<empname>Sunny Mathews</empname>
<address>5th Avenue Street</address>
</employee>
<employee type="Trainee">
<empid>A434567</empid>
<empname>Brad Hodge</empname>
<address>4th Avenue Street</address>
</employee>
<employee type="Senior Software Developer">
<empid>A433599</empid>
<empname>Glenn Powell</empname>
<address>6th Avenue Street</address>
</employee>
<employee type="Senior Software Developer">
<empid>A433588</empid>
<empname>Recordo Mathews</empname>
<address>6th Avenue Street</address>
</employee>
</class>
Please refer to a similar question
How to get specific XML elements with specific attribute value?
The following python script can also be used to get the details.
import xml.dom.minidom
import collections
xml_data = xml.dom.minidom.parse("test.xml")
emps = xml_data.getElementsByTagName("employee")
ssd = [emp.getAttribute('type') for emp in emps]
print collections.Counter(ssd)

Best way to create huge xml file by duplicating the element(including children) with dom or sax using Java

I have a xml file(ABC.xml) and i need to duplicate only the
<Transaction>...</Transaction>
multiple times(more than 100000 times) keeping the Header and Trailer intact creating NEW.xml whose final size may go upto 1GB. Also i have to increment the Uniqueid for every transaction in sequence.
As i m new to xml, i have been searching to this the best possible way, and im confused.
Can anyone please help me with the best way to do it(using DOM or SAX) and some piece of code.
Also can you please give me some links about it.
ABC.xml
========
<?xml version="1.0" encoding="UTF-8"?>
<Header><Datetime><date>20130209</date><Time>01:12</Time></Datetime></Header>
<Transaction>
<Uniqueid>1230001</Uniqueid>
<Affiliate>
<Name>abc</Name>
<Address>
<line1>aaaa</line1>
<line2>bbbb</line2>
<line3>cccc</line3>
</Address>
<Amount>123.00</Amount>
<Currency>USD</Currency>
<Purpose>
<line1>aaaa</line1>
<line2>bbbb</line2>
<line3>cccc</line3>
</Purpose>
</Affiliate>
</Transaction>
<Trailer><TotalTransactions>1</TotalTransactions><TotalAmount>123<TotalAmount> </Trailer>
NEW.xml
=======
<?xml version="1.0" encoding="UTF-8"?>
<Header><Datetime><date>20130209</date><Time>01:12</Time></Datetime></Header>
<Transaction>
<Uniqueid>1230001</Uniqueid>
<Affiliate>
<Name>abc</Name>
<Address>
<line1>aaaa</line1>
<line2>bbbb</line2>
<line3>cccc</line3>
</Address>
<Amount>123.00</Amount>
<Currency>USD</Currency>
<Purpose>
<line1>aaaa</line1>
<line2>bbbb</line2>
<line3>cccc</line3>
</Purpose>
</Affiliate>
</Transaction>
<Transaction>
<Uniqueid>1230002</Uniqueid>
<Affiliate>
<Name>abc</Name>
<Address>
<line1>aaaa</line1>
<line2>bbbb</line2>
<line3>cccc</line3>
</Address>
<Amount>123.00</Amount>
<Currency>USD</Currency>
<Purpose>
<line1>aaaa</line1>
<line2>bbbb</line2>
<line3>cccc</line3>
</Purpose>
</Affiliate>
</Transaction>
<Trailer><TotalTransactions>2</TotalTransactions><TotalAmount>246<TotalAmount></Trailer>
It would help if your source XML were well-formed - it needs an outer wrapper element.
There are a number of XQuery processors available in Java, for example Saxon. Just execute the query
<doc>{doc/Header, for $i in 1 to 100000 return doc/Transaction, doc/Footer}</doc>
on the supplied input document, assuming <doc> as the outer wrapper element.

How to parse xml file which is having another xml file as a tag in java?

I want to parse one xml file whose one of the tag/node having another xml file as a data so how could i parse that file.following is the xml file:
<?xml version="1.0"?>
<company>
<URL>
<RequestUrl>http://www.google.com
</RequestUrl>
<RequestData>
"<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"><v:Header /><v:Body><PingMe xmlns="visum.Server.Service" id="o0" c:root="1"><Data i:type="d:string">ping</Data></PingMe></v:Body></v:Envelope>"
</RequestData>
</company>
when i am going to parse the above xml file & retrieve the xml file under Request Data tag it is not returning me whole string of another xml file but only blank string so how should i proceed for that?
Parse this instead. You have to use CDATA in order for your extra "XML" to not be interpreted.
<?xml version="1.0"?>
<company>
<URL>
<RequestUrl>http://www.google.com</RequestUrl>
<RequestData>
<![CDATA[
<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"><v:Header /><v:Body><PingMe xmlns="visum.Server.Service" id="o0" c:root="1"><Data i:type="d:string">ping</Data></PingMe></v:Body></v:Envelope>
]]>
</RequestData>
</URL>
</company>

Categories

Resources