XML ImportNode Problem

  1. 4 days ago
    Edited 4 days ago

    I'm trying to import XML from one file, into another, and am admitting defeat. Tried every combination of every example in the docs, no avail.

    Two XML files. The first, xml1, has existing data with a structure of:

    <book>
    <structures>
    <structure name = "1" .... />
    <structure name = "2" .... />
    </structures>
    </book>

    xml2 is just a single structure "record" formatted and structured exactly like those in xml1:
    <structure name = "3" .... />

    I am unable to import or insert "<structure name = "3" .... />" from xml2 into xml1.

    Simply cannot crack the code. Spent more time than I should have so thought I'd ask!

    well, I'll do it like this:

    1. load in xml2 the structure name=3 like:

    Dim xml2 As New XMLDocument
    xml2.LoadXML struc3 'as string or folderItem

    2. load in xml1 the book like:

    Dim xml1 As New XMLDocument
    xml1.LoadXML book 'as string or folderitem

    3. import the root node from xml2 into root node of xml1, like:

      Dim theNode As XMLNode= xml1.ImportNode(xml2.FirstChild, True)
      xml1.FirstChild.FirstChild.AppendChild theNode
  2. Edited 4 days ago

    well, I'll do it like this:

    1. load in xml2 the structure name=3 like:

    Dim xml2 As New XMLDocument
    xml2.LoadXML struc3 'as string or folderItem

    2. load in xml1 the book like:

    Dim xml1 As New XMLDocument
    xml1.LoadXML book 'as string or folderitem

    3. import the root node from xml2 into root node of xml1, like:

      Dim theNode As XMLNode= xml1.ImportNode(xml2.FirstChild, True)
      xml1.FirstChild.FirstChild.AppendChild theNode
  3. Thank you for the assistance, Bernardo! Progress. I am able to add the record from xml2 into xml1, but not in the proper location. Based on what you wrote I think the problem is that there are other elements of other names before and after the "<structures .... />" and I need to be able to identify where in xml1 to append. My original post was very simplified and that made it less clear.

    What I need is something like the following pseudocode:

    Dim theNode As XMLNode= xml1.ImportNode(xml2.FirstChild, True)
     xml1.Child("structures").AppendChild theNode

    However I cannot find a way of directly addressing it.

    I guess I could use a loop to "find" the proper child and then .AppendChild to it?

    Best,

  4. Wow, I knew it had to be something simple. The finished code is below.

    Dim nodes As XmlNodeList
    Dim node As XmlNode
    
    node = xml1.ImportNode(xml2.FirstChild, True) ' import and return imported "node"
    nodes = xml1.XQL("//book/structures/structure") ' the collection of structure records
    nodes.Item(nodes.Length - 1).AppendChild(node) ' append to the end of the collection
  5. Sadly, the above code is not working in practice. Turns out that it inserts the new record INTO the last record. Not AFTER the last record and within bounds of the "structures" collection.

    :(

  6. What a journey. Final code. Simple.

    node = xml1.ImportNode(xml2.FirstChild, True) ' the imported node
    nodes = xml1.XQL("//book/structures") ' the collection of structure records
    nodes.Item(0).AppendChild(node) ' append to the end of the collection

or Sign Up to reply!