Create a new node from xPath

daverage

Active member
Joined
Aug 29, 2006
Messages
26
Programming Experience
Beginner
Hi all
Is it possible to create a node/element in an xml doc that from an xPath.

What I am doing is checking to see if it exists.
If it doesnt I want to create it from the xPath query.

For example

check "//Appearance//OnTop"
if it is nothing then use it to create the OnTop node

Is that possible?
 
XPath is a query language, ie only used to do lookups, but it can easily give you the node(s) to which you want to modify/append to etc. Use regular Xml methods to handle nodes.

Moved thread to Xml forum.
 
Thats cool, but can you give me any idea on how I would do that just from the path I am passing. I cant think how to do it without having to pass loads of other bits with it.
All i can think of is splitting the path and looping through it creating missing nodes as it loops. But again, cant think how to do it lol.
 
Example XmlDoc.SelectNodes("/root/items") give you a XmlNodeList consisting of zero or more single XmlNode nodes, the "/root/items" part is a XPath query, XmlDoc is the instance of the XmlDocument class loaded with a Xml file. Example XmlDoc.SelectSingleNode("/root/items[@id=123]/member") give you a single XmlNode. XmlDocument allows you to create a new XmlNode which you for instance can append to the node collection or single node you found. Except for these basic examples I don't understand what you ask.
 
Ok what I have is an path //Appearance//Top
First I check to see if it exists
so

saveNode("//Appearance//Top",xd)

function saveNode(xPath,XmlDoc)
if xd.selectsinglenode(xPath) is nothing then
'create missing nodes
end if
xd.save("doc.xml")
But I do not want to have to pass more than //Appearance//Top and the XmlDoc to the function.
 
I can do that, what I am having trouble getting is
I check
//Appearance//Top

It does not exist
so i need to then check the //Appearance exists
Then if it does I need to create //Top inside //Appearance

But this may be a large path
//Appearance//Font//Size
and if font or size do not exist i need to loop through and create them.

:(
 
Yes, you have to loop and check/create node by node, you can't create a path of nodes in one go. So walk the tree from root upwards and start appending as you find missing node.
 
haha my brain finally kicked in
thanks for the help, that link really did the trick!!

PHP:
saveNode("//Appearance//Note//Font",xd)
 
Function saveNode(ByVal xPath As String, ByVal xd As Xml.XmlDocument) As Xml.XmlNode
 If xd.SelectSingleNode(xPath) Is Nothing Then
  nodeFromPath(xPath, xd)
 End If
 Return (xd.SelectSingleNode(xPath))
End Function
Sub nodeFromPath(ByVal xPath As String, ByVal xd As Xml.XmlDocument)
 If xd.SelectSingleNode(xPath) Is Nothing Then
  Dim root As Xml.XmlNodeList
  Dim newNode As Xml.XmlNode
  Dim parentPath As String = Nothing
  Dim splitPath() As String
  Dim node As String
  Dim currNode As Xml.XmlNode
  Dim currentPath As String
  splitPath = Split(xPath, "//")
  For Each node In splitPath
   If node <> "" Then
    currentPath = parentPath
    parentPath &= "//" & node
    If xd.SelectSingleNode(parentPath) Is Nothing Then
     MsgBox(parentPath & " does not exist")
     root = xd.SelectNodes(currentPath)
     newNode = xd.CreateNode(Xml.XmlNodeType.Element, node, "")
     currNode = root(0).InsertAfter(newNode, root(0).ChildNodes(0))
    End If
   End If
  Next
  'clean up
  root = Nothing : newNode = Nothing : parentPath = Nothing : splitPath = Nothing : node = Nothing : currNode = Nothing : currentPath = Nothing
 End If
End Sub
 
Back
Top