473,322 Members | 1,538 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,322 software developers and data experts.

XML appending

I have a tricky question here.

1).I have an XML that is generated on the fly.

Expand|Select|Wrap|Line Numbers
  1. XML Generated
  2. -------------
  3.  
  4. Document genereatedDoc =
  5.  
  6. <Trade>
  7.     <Level>10 </Level>
  8.     <Sty> .5 </Sty>
  9. </Trade>
  10.  
  11.  
2).I have to add the above XML generated in between another XML document

[code]

----------------
Main XML Document
----------------

Document messagedoc =
<Interest>
<Market>
<Size>

<Size>
</Market>
</Interest>


--------------
Output shud be:
--------------
<Interest>
<Market>
<Size>
<Trade>
<Level>10 </Level>
<Sty> .5 </Sty>
</Trade>
<Size>
</Market>
</Interest>


[code]

3)I am using Java to add the document in 1 to 2.


Expand|Select|Wrap|Line Numbers
  1.  
  2. Java Code
  3. ---------
  4. Document genereatedDoc = 'The generated XML document in 1.
  5.  
  6. for (int i=0; i< genereatedDoc.getLength(); i++) {
  7. try {
  8.  
  9.     xpath = XPathFactory.newInstance().newXPath();
  10.  
  11.     XPathExpression expr   = xpath.compile("/Interest/Market/Size");
  12.     Node result = (Node)expr.evaluate(messagedoc, XPathConstants.NODE);
  13.  
  14.     NodeList nList = messagedoc.getElementsByTagName(result.getNodeName());
  15.     eL =(Element)nList.item(0);
  16.  
  17.     //Make a copy of the element including any child nodes.
  18.     Element e = (Element)eL.cloneNode(true);
  19.  
  20.     Element element = (Element)genereatedDoc.item(i);
  21.     Node dup =messagedoc.importNode(element, true);
  22.  
  23.     //Add this before Size
  24.     messagedoc.getDocumentElement().insertBefore(eL,eL.getNextSibling()).appendChild(dup);
  25.  
  26.  } catch (XPathExpressionException e) {
  27.  
  28.    }
  29.  
  30. }
  31.  
  32.  
It is unable to add the document to the child node as specified in the XPATH expression.
It adds the document only to the top level nodes like Interest,but not to the specific child node.
I want to add the generated document under element Size.

Please can somebody help me and tell me how to append the genereatedDoc after Size?
Jun 12 '07 #1
1 1556
dorinbogdan
839 Expert 512MB
You should call appendChild(dup) method on the node supposed to be the parent of the new node.
Jun 13 '07 #2

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

Similar topics

4
by: Bharat Bhushan | last post by:
Hi, I need to generate random bytes for x number of times and keep appending it to a bigger byte array. How can I do this ? for (int lctr=0; lctr < main.Main.NoOfattributes(); lctr=lctr+1){ ...
3
by: ketulp_baroda | last post by:
Hi I am having a problem in appending to sys.path I am doing it this way: >>> sys.path >>> sys.path.append(r'D:\Python23\Lib\site-packages\code') >>> sys.path But when I close the shell...
4
by: bucket79 | last post by:
Hi is there anyway appending to dictionary? list has this feature >>>a = >>>a.append(1) >>>print a but dictionary can't
1
by: Thomas Heller | last post by:
I want to append/insert additional data to an xml file. Context: I use gccxml to parse C header files. gccxml creates an xml file containing all the definitions from the header files. The xml...
5
by: rbt | last post by:
I know how to setup an empty list and loop thru something... appending to the list on each loop... how does this work with dicts? I'm looping thru a list of files and I want to put the file's...
1
by: dmiller23462 | last post by:
Hey guys.... I put an error-handling in my page and have it posted at the complete end of the code, see below(when people were putting in 's I was getting the delimiter errors). Great, I...
3
by: MLH | last post by:
I have a query, qryAppend30DayOld260ies that attempts to append records to tblCorrespondence. When run, it can result in any of the following: appending no records, appending 1 record or appending...
4
by: John A Grandy | last post by:
could someone explain the following to me : Appending the literal type character I to a literal forces it to the Integer data type. Appending the identifier type character % to any identifier...
1
by: Frank | last post by:
Hi, Let's say I have a file named myFile.xml Within that file I have blocks of data which I'd like to add at different times during the day. e.g. <LogEntry>
2
by: sarada purkait | last post by:
hii i have to write into a file from the start and then go on appending to it .. i tried using ( ios::out|ios::app) but by this the file keeps on appending every time i run the program and the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.