Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
| Search | Today's Posts | Mark Forums Read
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 18th, 2003, 01:32 AM
Registered User
 
Join Date: Jun 2003
Location: B\'ham, Alabama, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Saving the parent node

I am appending a child node and I am using a variable called "xmlDoc" as the main DomDocument. As I drill down to the node I need to append xmlDoc is only pointing at each particular node on the document.

So after I have appended my file xmlDoc is pointing to that node and when I go to save the file (xmlDoc.Save) I lose everything except the node that I just appended.

Here is the code below, it is in VB. I am stepping through the child nodes to get to the node "observation". I append the node and then I can only save that node. Ofcourse what I need to do is to save the entire file and not just the latest node I have drilled down to.
Code:
Public Sub UpdateXML(RadId, ObId, AzType, DisType, StartPnt, EndPnt, _
    Bearing, RndBearing, Distance, Layer, Symbol, isObserved)
   On Error GoTo ErrHandler:

   Dim xmlDoc        As DOMDocument
   Dim nodeSection   As IXMLDOMNode  
   Dim ObjElement    As IXMLDOMElement
   Dim NodeName      As String
   Dim x As Integer
   Dim y As Integer

   Set xmlDoc = New DOMDocument
   xmlDoc.async = False
   xmlDoc.Load (PathName)

   Set objDom = xmlDoc
   Set ObjElement = xmlDoc.documentElement

    For x = 0 To ObjElement.childNodes.Length - 1
        If "RadialDetails" = ObjElement.childNodes.item(x).NodeName Then Exit For
    Next x

    xmlDoc.loadXML _
        (ObjElement.childNodes.item(x).xml)
    Set ObjElement = xmlDoc.documentElement

    For x = 0 To ObjElement.childNodes.Length - 1
        If RadId = ObjElement.childNodes.item(x).Attributes(0).text  Then Exit For
    Next x

    xmlDoc.loadXML _
        (ObjElement.childNodes.item(x).xml)
    Set ObjElement = xmlDoc.documentElement

    Set nodeSection = ObjElement.selectSingleNode("Observations")
    'if found append Point information
    If Not nodeSection Is Nothing Then
        nodeSection.appendChild CreateXML(xmlDoc, ObId, AzType, DisType, StartPnt, EndPnt, _
        Bearing, RndBearing, Distance, Layer, Symbol, isObserved)

        'line return 
        nodeSection.appendChild nodeSection.ownerDocument.createTextNode(vbCrLf)

        xmlDoc.Save (PathName)
    Else
        MsgBox "Incomplete or missing file! Could not save details to file:" & vbCrLf & _
        PathName, vbCritical, "Error"
    End If
exithere:
    Set xmlDoc = Nothing
    Set ObjElement = Nothing
    Set nodeSection = Nothing

    Exit Sub
ErrHandler:
    MsgBox "Error in clsXMLWorker.UpdateRadialXML" & vbCrLf & _
    Err.Description & " " & Err.Number
    Dim Form As Form
    For Each Form In Forms
        Unload Form
    Next
    End
End Sub
Can some one tell me how to back back out to the parent node before I save so I can save the entire file?

If you don't succeed you run the risk of failure. *Dan Quale
 
Old December 18th, 2003, 05:31 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

The way you are doing things you cannot back out to the parent, because you have overwritten the original xml document with just the fragment you have created.

You don't need to do things like this:
xmlDoc.loadXML _
        (ObjElement.childNodes.item(x).xml)
    Set ObjElement = xmlDoc.documentElement

what you can do instead is just
Set ObjElement = ObjElement.childNodes.item(x)
then you still have the element you want in ObjElement *and* you still have the whole xml document in xmlDoc.

Also, make sure your CreateXML function is using the original xmlDoc and its createElement() or createNode() methods to create the child node.

Finally, I think you would benefit from learning XPath and the selectNodes() or selectSingleNode() methods. Its much more efficient than looping through all child nodes to find a certain node. For example you could do
Set ObjElement = xmlDoc.selectSingleNode("/RadialDetails[@id='" & RadID & "']/Observations")
to go straight to the element you want (I'm guessing the exact path here because you don't show us what your xml looks like).

hth
Phil
 
Old December 23rd, 2003, 07:05 PM
Registered User
 
Join Date: Jun 2003
Location: B\'ham, Alabama, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks! I quit reloading the document and that took care of the problem.

I plan on learing all I can about XPath and the rest just as soon as I get a chance, hopefully in the next month or so.

Thanks again.

If you don't succeed you run the risk of failure. *Dan Quale




Similar Threads
Thread Thread Starter Forum Replies Last Post
Introducing parent node to specific siblings Frimann XSLT 11 July 3rd, 2008 03:20 PM
Copy parent node and not its children bonekrusher XSLT 4 August 29th, 2007 08:44 AM
retrieving information from a parent node Tomi XSLT 2 September 6th, 2006 06:54 AM
Finding parent node Chamkaur XSLT 4 August 9th, 2006 06:26 AM
How to delete an element and parent node. crossedge XSLT 1 March 14th, 2006 05:10 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.