aspose file tools*
The moose likes XML and Related Technologies and the fly likes Extract db data into a XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Extract db data into a XML" Watch "Extract db data into a XML" New topic
Author

Extract db data into a XML

Thillai Sakthi
Ranch Hand

Joined: Jun 17, 2000
Posts: 102
Dear All:
Consider this scenario. Have got a xml file. whose data have been loaded to bunch of tables (not the entire xml has been loaded but only 25% of it, rest are intact in xml file). Have updated the tables...changed some data. Now that xml file and db data has gone out of synch, would like to generate a new xml file putting together data from the original xml file as well as modified data from db....
Am finding it difficult how do I parse one xml and write another xml file maintaining the same structure.
Can anyone pls help?


Regards,
Shakthi
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Could you describe the structure of your XML file and the changes that you need to be able to merge into it? Also, is this XML file small, big, huge, what?
It's probably easiest to read the XML into a Java object model, read the database changes into a similar object model, and then Just Write The Logic for merging the two.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Thillai Sakthi
Ranch Hand

Joined: Jun 17, 2000
Posts: 102
The xml file's structure is very deep and the attribute values will be changing in the data base, which we need to capture. For example, consider this piece from the xml file....
[CODE]
<UML:Class name="Person" xmi.id="EAID_620A1B15" visibility="public" namespace="EAPK_E977D80E" isRoot="false" isLeaf="false" isAbstract="false" isActive="false">
<UML:ModelElement.stereotype>
<UML:Stereotype name="eai.person"/>
</UML:ModelElement.stereotype>
<UML:ModelElement.taggedValue>
<UML:TaggedValue tag="isSpecification" value="false"/>
<UML:TaggedValue tag="ea_stype" value="Class"/>
<UML:TaggedValue tag="ea_ntype" value="0"/>
<UML:TaggedValue tag="version" value="1.0"/>
<UML:TaggedValue tag="package" value="EAPK_E977D80E"/>
<UML:TaggedValue tag="date_created" value="2003-05-07 14:54:28"/>
<UML:TaggedValue tag="date_modified" value="2003-10-29 12:46:47"/>
<UML:TaggedValue tag="gentype" value="Java"/>
<UML:TaggedValue tag="tagged" value="1"/>
<UML:TaggedValue tag="package_name" value="Meta Model"/>
<UML:TaggedValue tag="phase" value="1.0"/>
<UML:TaggedValue tag="complexity" value="1"/>
<UML:TaggedValue tag="status" value="Proposed"/>
<UML:TaggedValue tag="stereotype" value="eai.person"/>
<UML:TaggedValue tag="style" value="BackColor=-1;BorderColor=-1;BorderWidth=-1;FontColor=-1;VSwimLanes=0;HSwimLanes=0;BorderStyle=0;"/>
</UML:ModelElement.taggedValue>
</UML:Class>
[CODE]
Here the attributes that are captured in the db are:
name, xmi.id, visibility, namespace of UML:Class and
phase, version, complexity, stereotype of UML:TaggedValue.
All other values are not stored in db. These captured values in db are changeable. So, when re-generating the xml from revised data in db, i need to maintain the info in xml which are not initially stored to db, then only xml file would be complete.
The xml file is huge its size can be go up to couple of Megs.
I would like to know more abt the Java data model.. could u pls give me more info on that?
TIA
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Ok. The XMI looks like simple and small enough for reading into memory in full. Here's how I would do it:
1) Parse the XML file into a org.w3c.dom.Document using DocumentBuilder#parse()
2) Do Document#getElementsByTagName() to get a list of all UML:Class nodes
3) Iterate through each returned Class node
4) Check each attribute's value in the database, calling Element#setAttribute() to replace the old value if the two are out-of-synch
5) Write the Document back to the disk
[ February 05, 2004: Message edited by: Lasse Koskela ]
Thillai Sakthi
Ranch Hand

Joined: Jun 17, 2000
Posts: 102
Originally posted by Lasse Koskela:

5) Write the Document back to the disk
[ February 05, 2004: Message edited by: Lasse Koskela ]

Thanks Lasse. I am able to perform all things above except Step (5). If I write doc.toString(), it writes only

How do I write this into the disk? (I am using FileWriter to write the document into a xml file)
TIA
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Take a look at this and this.
Thillai Sakthi
Ranch Hand

Joined: Jun 17, 2000
Posts: 102
Originally posted by Lasse Koskela:
Take a look at this and this.

Awesome Lasse.....It works fantastic!
Thanks a lot....
SP Nam
Ranch Hand

Joined: Oct 15, 2003
Posts: 41
posted October 06, 2004 06:17 AM
--------------------------------------------------------------------------------
Hi all

I am developing a messaging application in xml which is extensively used to transfer data between database, middle tier and client tier.

From client side I will get xml string without any table names (its like xml document). I have to extract values w.r.t column namesfrom xml string and insert the values into Oracle database. Can anyone send a snippet of code or links to achieve this functionality, its bit urgent.

please its urgent.

Respond immediately.

Praveen kumar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Extract db data into a XML
 
Similar Threads
Data get corrupted
using xml or query to display
Tackling large size XML files
XML/XSD to Database using Java
Architectural consideration.