Combine XML documents

I have an app that generates the project file by writing an XML document for each individual object. This is because my app allows to export the individual objects aswell as whole project file containing all of the objects.

If my code is written like this:
Dim xml As XmlDocument
Dim obj As XMLNode

xml = New XmlDocument
resource = xml.AppendChild(xml.CreateElement(“obj”))
resource.SetAttribute(“Name”, name)
resource.SetAttribute(“ID”, str(ID))
return xml

From what is returned from this method what should I do to attach it to another XML document. In my case, I am, as I said putting all of the individual objects in a project file.

Thanks

Yeah that’s going to be painful to make sure its still valid XML once you combine it all

You’d be better off writing it as XML or VCP and making all the other objects external

I read the question as referring to “project file” as the application’s “project,” not a Xojo project. If Norman is correct, and you do mean a Xojo “project,” please ignore the following, Oliver…

What I do in cases like this is to separate the creation of the XMLDocument from the code the creates an object’s XMLElement.

I’m assuming you have a method somewhere that is responsible for creating the export, and that knows about the object(s) that need to be included in the export. Create the XMLDocument in that method, create any child element that will hold the objects, and then pass that immediate parent node to a method of each object to add itself to the XML structure.

[code]dim myXML as NEW XMLDocument
dim myRootNode, myParentNode as XMLNode
dim i,n as integer
dim myObject as clsXMLAwareObject // this could be a super class, or a class interface

myRootNode = myXML.createElement(“root”)
myXML.appendChild(myRootNode)
myParentNode = myXML.createElement(“objects”)
myRootNode.appendChild(myParentNode)
// add whatever other elements, attributes, etc. that you need (if any) on the document,
// root node, or parent node here

n = mObjectArr.uBound // mObjectArr is defined somewhere outside the method as clsXMLAwareObject()
for i = 0 to n
myObject = mObjectArr(i)
myObject.addYourselfToXMLNode(myParentNode)
next

// do whatever you want to with myXML here
[/code]
In clsXMLAwareObject, create the method addYourselfToXMLNode(theNode as XMLNode) something like this:

if theNode <> NIL then dim myNode as xmlNode = theNode.ownerDocument.createElement("obj") myNode.SetAttribute("Name", me.mName) myNode.SetAttribute("ID", str(me.mID)) theNode.appendChild(myNode) end if
I would also, then, create a constructor on each obj class/subclass which takes an XMLElement as a parameter (theNode as XMLNode) so that the objects can be reconstructed from the XML Document:

if theNode <> NIL then dim myIDStr, myNameStr as string myIDStr = theNode.getAttribute("ID") myNameStr = theNode.getAttribute("Name") if myIDStr <> "" then me.mID = val(myIDStr) // possibly add error handling for an empty ID? if myNameStr<> "" then me.mName = myNameStr // possibly add error handling for an empty name? end if
This will allow you to select any subset of all your objects, and export them without needing to combine XMLDocuments. It also means that once you’ve created and debugged the object’s (or objects’) method(s) to add a node to the parent node, you need rarely, if ever, look at the code again :stuck_out_tongue:

[quote=46624:@Peter Truskier]I read the question as referring to “project file” as the application’s “project,” not a Xojo project. If Norman is correct, and you do mean a Xojo “project,” please ignore the following, Oliver…

What I do in cases like this is to separate the creation of the XMLDocument from the code the creates an object’s XMLElement.

I’m assuming you have a method somewhere that is responsible for creating the export, and that knows about the object(s) that need to be included in the export. Create the XMLDocument in that method, create any child element that will hold the objects, and then pass that immediate parent node to a method of each object to add itself to the XML structure.

[code]dim myXML as NEW XMLDocument
dim myRootNode, myParentNode as XMLNode
dim i,n as integer
dim myObject as clsXMLAwareObject // this could be a super class, or a class interface

myRootNode = myXML.createElement(“root”)
myXML.appendChild(myRootNode)
myParentNode = myXML.createElement(“objects”)
myRootNode.appendChild(myParentNode)
// add whatever other elements, attributes, etc. that you need (if any) on the document,
// root node, or parent node here

n = mObjectArr.uBound // mObjectArr is defined somewhere outside the method as clsXMLAwareObject()
for i = 0 to n
myObject = mObjectArr(i)
myObject.addYourselfToXMLNode(myParentNode)
next

// do whatever you want to with myXML here
[/code]
In clsXMLAwareObject, create the method addYourselfToXMLNode(theNode as XMLNode) something like this:

if theNode <> NIL then dim myNode as xmlNode = theNode.ownerDocument.createElement("obj") myNode.SetAttribute("Name", me.mName) myNode.SetAttribute("ID", str(me.mID)) theNode.appendChild(myNode) end if
I would also, then, create a constructor on each obj class/subclass which takes an XMLElement as a parameter (theNode as XMLNode) so that the objects can be reconstructed from the XML Document:

if theNode <> NIL then dim myIDStr, myNameStr as string myIDStr = theNode.getAttribute("ID") myNameStr = theNode.getAttribute("Name") if myIDStr <> "" then me.mID = val(myIDStr) // possibly add error handling for an empty ID? if myNameStr<> "" then me.mName = myNameStr // possibly add error handling for an empty name? end if
This will allow you to select any subset of all your objects, and export them without needing to combine XMLDocuments. It also means that once you’ve created and debugged the object’s (or objects’) method(s) to add a node to the parent node, you need rarely, if ever, look at the code again :P[/quote]
Thanks but I mean my app’s project file.