473,399 Members | 3,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Using importNode to add 2 XML docs. Please see and Advise

An extremely tricky and challenging code.

I have been able to add 2 XML documents together
using importNode().

Expand|Select|Wrap|Line Numbers
  1.  
  2. xmlDoc1
  3. ----------
  4.  
  5. <?xml version="1.0" encoding="UTF-8"?>
  6. <i:Interest xmlns:i="common">
  7. <i:Details xsi.type="i:vanilla.details.stock">
  8.   <i:Strategy xsi.type="strategies:call">
  9.      <strategies.Expiry>2009-12-01</strategies.Expiry>
  10.   </i:Strategy>
  11. <i:Ref/>
  12. </i:Details>
  13. </i:Interest>
  14.  
  15. xmlDoc2
  16. ------------
  17. <?xml version="1.0" encoding="UTF-8"?>
  18. <i:Underlying value="KGF">
  19. <common:Currency>GBP</common:Currency>
  20. </i:Underlying>
  21.  
  22. My Java Code to append them together
  23. -----------------------------------------------------
  24.  
  25. NodeList list = xmlDoc2.getChildNodes();
  26.  
  27. for (int i=0; i< list .getLength(); i++) {
  28.  
  29.      Element element = (Element)list.item(i);
  30.      Node dup = xmlDoc1.importNode(element, true);
  31.  
  32.      xmlDoc1.getDocumentElement().appendChild(dup);
  33. }
  34.  
  35. Output:
  36. ------    
  37. <?xml version="1.0" encoding="UTF-8"?>
  38. <i:Interest xmlns:i="common">
  39. <i:Details xsi.type="i:vanilla.details.stock">
  40.      <strategies.Description>Call</strategies.Description>
  41.      <strategies.Expiry>2009-12-01</strategies.Expiry>
  42.    </i:Strategy>
  43. <i:Ref/>
  44. </i:Details>
  45. <i:Underlying value="KGF">
  46. <common:Currency>GBP</common:Currency>
  47. </i:Underlying>
  48. </i:Interest>    
  49.  
  50.  
  51.  
Query:
I would like to add the 2nd XML Document after element <i:Ref> . How to do this? I dont want the 2nd XML doc after <i:details> but before it.
Please help.
May 3 '07 #1
5 1859
Can someone please advise me on the above issue I am facing please?
May 4 '07 #2
dorinbogdan
839 Expert 512MB
So you want to add the new element at the end of the Details node.
Since Details node is the first child of root element, try:
Expand|Select|Wrap|Line Numbers
  1. xmlDoc1.getDocumentElement().getFirstChild().appendChild(dup);
Or can use XPath, to find the node to append to, see this link.
May 4 '07 #3
Thanks a lot for this Doran,This works perfect.

Just one more query

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <i:Interest xmlns:i="common">
  3. <i:abc> <i:abc>
  4. <i:Details xsi.type="i:vanilla.details.stock">
  5.   <i:Strategy xsi.type="strategies:call">
  6.      <strategies.Expiry>2009-12-01</strategies.Expiry>
  7.   </i:Strategy>
  8. <i:Ref/>
  9. </i:Details>
  10. </i:Interest>
  11.  
If i:details is not the first child but could be down the XML Doc
then do I need to use XPATH to search for this element and
append after this?
May 4 '07 #4
Hi Doran, I have been able to run this now
Thanks for all your help.
Its running fine.
May 4 '07 #5
dorinbogdan
839 Expert 512MB
If i:details is not the first child but could be down the XML Doc
then do I need to use XPATH to search for this element and
append after this?
Yes, you can use XPath, or loop through the nodes until find the right one.
The appending (appendChild()) must be done (called) on the element you want to insert into.
May 4 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Kenneth McDonald | last post by:
If this is not an appropriate newsgroup for this type of posting, please let me know and (if possible) suggest an alternative. I've done a fair bit of research on the net, but information is...
0
by: Tawfik Rady | last post by:
Hi Everybody, 1- I am trying to upsize an access database to my MySQL server on my website. I tried to get connected within access and failed. Please advise. I am using Win XP as the operating...
0
by: Andy Fish | last post by:
Hi, I have server-based Java application that maintains a fairly large XML structure as a DOM. In response to a user request I want to select various nodes (including sub-nodes) and jam them...
3
by: adam | last post by:
Hi, I'm building a tree control that lazily loads branches of the tree using the document.load() method. The external XML document that is loaded is generated by a servlet as XHTML. What I...
1
by: Peter Nofelt | last post by:
Hey All, I'm running into this issue with parsing through an xml document by tag name. Below is an example xml document: File Name: things.xml <things> <people> <name>Peter</name>
5
by: yawnmoth | last post by:
I wrote the following in an attempt ot better understand importNode: <script> f1=document.createElement("iframe"); f1.src=unescape("http://www.domain.tld/test.txt");...
10
by: Simon Brooke | last post by:
The DOM API has included public Node importNode(Node,boolean) as a method of the Document interface for a long time. Does anything actually implement it? Xerces 2 is giving me: ...
0
by: luthriaajay | last post by:
An extremely tricky and challenging code. I have been able to add 2 XML documents together using importNode(). xmlDoc1 ---------- <?xml version="1.0" encoding="UTF-8"?>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.