CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2008
    Posts
    42

    creation of xml doc with proper formatting

    Dim objDOM As New MSXML2.DOMDocument40
    Dim objNode As MSXML2.IXMLDOMNode
    Dim objChildNode As MSXML2.IXMLDOMNode
    Dim objGrandChildNode As MSXML2.IXMLDOMNode
    Dim objAttribute As MSXML2.IXMLDOMAttribute
    Dim objElement As MSXML2.IXMLDOMElement

    ' Create the main xml node
    Set objNode = objDOM.createNode(NODE_PROCESSING_INSTRUCTION, "xml", "")
    objDOM.appendChild objNode

    ' Create the an attribute name is "name" and value is "nguyen"
    Set objAttribute = objDOM.createAttribute("name")
    ' Set this attribute "name" to the value of "nguyen"
    objAttribute.Text = "Jane Doe"

    ' Create the Parent Node - "Root"
    Set objNode = objDOM.createNode(NODE_ELEMENT, "Root", "")

    ' Create a child node - "ChildofRoot"
    Set objChildNode = objDOM.createNode(NODE_ELEMENT, "ChildofRoot", "")

    ' Set an IXMLDOMElement to set an attribute
    ' the "RootChild" node will house the attribute - "name" from above"
    Set objElement = objChildNode
    objElement.setAttributeNode objAttribute
    Set objAttribute = Nothing

    ' Create a Grand Child Node - "GrandChildofRoot"
    Set objGrandChildNode = objDOM.createNode(NODE_ELEMENT, "GrandChildofRoot", "")
    ' Set this node "GrandChildofRoot" to "TESTVALUE"
    objGrandChildNode.Text = "TESTVALUE"

    ' Append "GrandChildofRoot" to "ChildofRoot"
    objChildNode.appendChild objGrandChildNode
    Set objGrandChildNode = Nothing

    ' Append "ChildofRoot" to "Root"
    objNode.appendChild objChildNode
    Set objChildNode = Nothing

    ' Append "Root" to the XML Dom Document
    objDOM.appendChild objNode
    Set objNode = Nothing

    MsgBox objDOM.xml

    Set objDOM = Nothing

    creating a xml dcoument from database thr' code in vb6.0 & validating it against xsd using msxml4.0
    everything working fine
    the xml file is formed from the code / the data i get during runitme
    when i validate it it gives me the validation results but it seems that the entire xml doument is in one line though the xml document tags appears in diferent lines
    as when i validate it for multiple error it gives me every time line 1 but change in column no

    i made the same file wth same data manually & validated it
    in that i get the proper line no & column no
    suppose the error is in line 10 i get line no 10 column no 5

    but the file that is generated dynamically gives me error but taking the whole document as line 1
    how do i recitfy it so that i get the proper line no
    though it appears properly in the browser ie all the nodes, subnodes, elements appears propelry as needed

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: creation of xml doc with proper formatting

    Please use [ Code] tags when posting code!

  3. #3
    Join Date
    May 2009
    Location
    London
    Posts
    51

    Re: creation of xml doc with proper formatting

    Agree with Hannes who is indeed Great, please use code tags.


    If you are looking for something that will generate a more readable format for your XML file ie each node on a seperate line and indented.

    Have a look at this .....
    http://www.vb-helper.com/howto_forma..._document.html

    If you copy the FormatXmlDocument and FormatXmlNode functions into your code and then add this line before your message box.
    Code:
    FormatXmlDocument objDOM
    Seems to generate a formatted XML.

  4. #4
    Join Date
    Oct 2008
    Posts
    42

    Re: creation of xml doc with proper formatting

    ok

    i checked the link but not help me
    i hope my probelm is clear

    just want to locate the excat error postion after xml validation code using sax parser
    the xml creation is done using vb6.0 code in which all the tags appears as a single line when opened in text editor but in browser it appears properly . the nodes in the browser is indented

  5. #5
    Join Date
    May 2009
    Location
    London
    Posts
    51

    Re: creation of xml doc with proper formatting

    Using your supplied code will generate the following XML file.
    Code:
    <?xml version="1.0"?>
    <Root><ChildofRoot name="Jane Doe"><GrandChildofRoot>TESTVALUE</GrandChildofRoot></ChildofRoot></Root>
    If you open this XML file in your browser then the browser will display the results formatted like this
    Code:
     <?xml version="1.0" ?> 
    - <Root>
      - <ChildofRoot name="Jane Doe">
          <GrandChildofRoot>TESTVALUE</GrandChildofRoot> 
        </ChildofRoot>
      </Root>
    Apologies if I have misunderstood your question but your problem seems to be that VB6 XML will generate the file with all of the nodes in the second line of the file which is why your parser is always reporting the error on the same line.

    If you add the fuctions listed in my previous post and then call the FormatXmlDocument fuction before saving the XML object then the output file will look like this.
    Code:
    <?xml version="1.0"?>
    <Root>
        <ChildofRoot name="Jane Doe">
            <GrandChildofRoot>TESTVALUE</GrandChildofRoot>
        </ChildofRoot>
    </Root>
    With the file in this format the parser should generate the expected line numbers.

    I haven't done a great deal of work with XML files but I had this problem a few months ago and the files that VB6 XML was originally generating were an absolute nightmare to edit in Notepad.

    Hope this helps.

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: creation of xml doc with proper formatting

    Here's a class that you can use...
    Attached Files Attached Files
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

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

Featured