• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

How to copy node from one XML file to another file

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to XML. I hope you guys can help me figuring out this problem. I am trying to figure out how to do the following :
See the following small XML file.( I call it one.xml)
<?xml version="1.0" encoding="UTF-8"?>
<addressbook>
<friends>
<friend name="xyz">
<address>
<apt>A-123</apt>
<street>Burger St.</street>
<city>San Carlos</city>
<state>CA</state>
<zip>92042</zip>
</address>
</friend>
<friend name="abc">
<address>
<apt>111</apt>
<street>Bagel St.</street>
<city>San Diego</city>
<state>CA</state>
<zip>92049</zip>
</address>
<phone>
<cell>111-222-3333</cell>
<cell>111-222-4444</cell>
</phone>
</friend>
</friends>
</addressbook>
See the following small XML file.( I call it two.xml)
<?xml version="1.0" encoding="UTF-8"?>
<addressbook>
<friends>
<friend name="xyz">
<address>
<apt>A-123</apt>
<street>Burger St.</street>
<city>San Carlos</city>
<state>CA</state>
<zip>92042</zip>
</address>
</friend>
<friend name="abc">
<address>
<apt>111</apt>
<street>Bagel St.</street>
<city>San Diego</city>
<state>CA</state>
<zip>92049</zip>
</address>
</friend>
</friends>
</addressbook>

As you can see, one.xml has phone element in one of the friend's details but in the two.xml, it is not there. What I want to do is write a program which will copy the phone node and subnodes in one.xml to their corresponding location in two.xml.
Any ideas ? or example programs ?
Thanks.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following should help -
https://coderanch.com/t/124039/XML/importNode-cannot-resolve-symbol
Cheers,
Dan
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You can do that in two ways..
1) Read the First XML File using SAX Api and get the Phone Number details and create a DOM Tree on the second XML File and insert the Node <phone> in the appropriate location..
2) Using JAXB is the second option..

Unmarshall the First and second XML Files and ise the get() method to fetch the Phone Number from the First XML and set the Phone Number and Marshall the root Object..
For this to work you need to define the <phone> as an optional element in your second XML 's DTD..
Hope this helps..

-Sateesh
 
I didn't say it. I'm just telling you what this tiny ad said.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic