Results 1 to 6 of 6

Thread: xml troubles

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    xml troubles

    I have this code
    VB Code:
    1. Private Sub Form_Load()
    2. Dim objDoc As MSXML2.DOMDocument 'this is a dom document which stores your xml
    3. Dim objRootElement As IXMLDOMElement ' Create a root xml element
    4. Dim objCurrentElement As IXMLDOMElement
    5. Dim i As Integer
    6.  
    7.     Set objDoc = New DOMDocument
    8.    
    9.     Set objRootElement = objDoc.createElement("root")
    10.     objDoc.appendChild objRootElement
    11.    
    12.     For i = 1 To 3
    13.         Set objCurrentElement = objDoc.createElement("header")
    14.         objRootElement.appendChild objCurrentElement
    15.        
    16.         objCurrentElement.setAttribute "name", "entry" & i
    17.         objCurrentElement.setAttribute "parent", "entry" & i - 1
    18.         objCurrentElement.setAttribute "text", "This is entry " & i
    19.     Next i
    20.    
    21.     Debug.Print objDoc.xml
    22. End Sub

    This will create some xml that looks like this
    PHP Code:
    <root>
        <
    header name="entry1" parent="" text="This is entry 1"/>
        <
    header name="entry2" parent="" text="This is entry 2"/>
        <
    header name="entry3" parent="" text="This is entry 3"/>
    </
    root
    What I need to do now is change this so I can insert a new line so it looks like this.
    PHP Code:
    <root>
        <
    header name="entry1" parent="" text="This is entry 1"/>        
        <
    header name="entry2" parent="" text="This is entry 2"/>
            <
    menuitem name="sub1" parent="entry2">Added Item<menuitem/>
        <
    header name="entry3" parent="" text="This is entry 3"/>
    </
    root
    How do you go about inserting a new element where you need to?

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    VB Code:
    1. Public Enum NodeType
    2.     NODE_INVALID = 0
    3.     NODE_ELEMENT = 1
    4.     NODE_ATTRIBUTE = 2
    5.     NODE_TEXT = 3
    6.     NODE_CDATA_SECTION = 4
    7.     NODE_ENTITY_REFERENCE = 5
    8.     NODE_ENTITY = 6
    9.     NODE_PROCESSING_INSTRUCTION = 7
    10.     NODE_COMMENT = 8
    11.     NODE_DOCUMENT = 9
    12.     NODE_DOCUMENT_TYPE = 10
    13.     NODE_DOCUMENT_FRAGMENT = 11
    14.     NODE_NOTATION = 12
    15. End Enum
    16.  
    17.  
    18.     Dim oxmlChildNode As IXMLDOMNode
    19.        
    20.  
    21. Set oxmlChildNode = XMLdoc.createNode(NODE_ELEMENT, "TypeOfNode", "")
    22.         oxmlChildNode.Text = "Some Text"
    23.         oxmlNode.appendChild oxmlChildNode

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    Ok, here is what I’m trying to do. I have a table that contains information about a menu.

    From this table I need to create an xml file that has the same hierarchy as I’m going to have when the menu is created. For example, let’s assume that this is the final menu structure that I’m looking for. The items in the parentheses are the menu names.

    Format (mnuFormat)
    ----Align (mnuFormatAlign)
    --------Left (mnuFormatAlignLeft)
    --------Center (mnuFormatAlignCenter)
    --------Right (mnuFormatAlignRight)
    ----Make Same Size (mnuFormatSize)
    --------Height (mnuFormatSizeHeight)
    --------Width (mnuFormatSizeWidth)
    --------Both (mnuFormatSizeBoth)
    Debug (mnuDebug)
    ----Step into (mnuDebugInto)
    ----Step Over (mnuDebugOver)
    Run (mnuRun)
    ----Start (mnuRunStart)
    ----Start With Full Compile (mnuRunFullCompile)

    I would like the xml file that I generate to look like this.

    PHP Code:
    <menu>
        <
    menuheading name="mnuFormat" parent="" text="Format">
            <
    menuitem name="mnuFormatAlign" parent="mnuFormat" text="Align">
                <
    item name="mnuFormatAlignLeft" parent="mnuFormatAlign" text="Left"/>
                <
    item name="mnuFormatAlignCenter" parent="mnuFormatAlign" text="Center"/>
                <
    item name="mnuFormatAlignRight" parent="mnuFormatAlign" text="Right"/>
            </
    menuitem>
            <
    menuitem name="mnuFormatSize" parent="mnuFormat" text="Make Same Size">
                <
    item name="mnuFormatSizeHeight" parent="mnuFormatSize" text="Height"/>
                <
    item name="mnuFormatSizeWidth" parent="mnuFormatSize" text="Width"/>
                <
    item name="mnuFormatSizeBoth" parent="mnuFormatSize" text="Both"/>
            </
    menuitem>
        </
    menuheading>
        <
    menuheading name="mnuDebug" parent="" text="Debug">
            <
    item name="mnuDebugInto" parent="mnuDebug" text="Step Into"/>
            <
    item name="mnuDebugOver" parent="mnuDebug" text="Step Over"/>
        </
    menuheading>
        <
    menuheading name="mnuRun" parent="" text="Run">
            <
    item name="mnuRunStart" parent="mnuRun" text="Start"/>
            <
    item name="mnuRunFullCompile" parent="mnuRun" text="Start With Full Compile"/>
        </
    menuheading>
    </
    menu
    I assume that I will have to create some recursive function that will go through the recordset and add nodes based on its parent.

    The problem that I’m having is….
    How do I find a node based on the name attribute (these are unique) and then insert a child node that contains attributes after that node?

    With the code that you gave me Martin, I can insert a child node but can’t figure out how to add attributes to it. It is also placing it after the last element created. I can’t figure out how to tell it where to place it.
    Attached Images Attached Images  

  4. #4

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    Originally posted by MartinLiss
    I'm having trouble understanding what you are starting with. Are you creating the xml file from the menu?
    Ultimately I need to create the xml from a table.

    Currently, what I have is an asp page that has a menu that is being populated using a static xml file and rendered using an xsl file.

    Now I need to give the users options on what menu items they want to see. I’m moving away from a static xml file to a table driven approach. The user choice will be stored in a users table. Let’s say for example the user only wants to see a menu like this

    Format (mnuFormat) 1
    ----Make Same Size (mnuFormatSize) 5
    --------Height (mnuFormatSizeHeight) 9
    --------Width (mnuFormatSizeWidth) 10
    --------Both (mnuFormatSizeBoth) 11
    Run (mnuRun) 3
    ----Start (mnuRunStart) 14

    When the user logs on, the user’s options are retrieved from a users table and their menu choice are used to create a record set. This record set will contain all the menu entries that I have to generate xml for.

    SELECT *
    FROM tbl_menu
    WHERE (MenuID IN (1,5,9,10,11,3,14))

    From this record set I need to build the menu xml. It needs to have the hierarchy that you see above in order for it to work with the xsl that we currently have in place.

    Does this explanation help any?

  6. #6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width