Results 1 to 4 of 4

Thread: Creating an XML file

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Creating an XML file

    Hi, I'm trying to create a XML file that looks exactly like the one below.

    Code:
    <?xml version="1.0" encoding="iso-8859-1" ?>
    <!DOCTYPE nzb PUBLIC "-//newzBin//DTD NZB 1.0//EN" "http://www.newzbin.com/DTD/nzb/nzb-1.0.dtd">
    <nzb xmlns="http://www.newzbin.com/DTD/2003/nzb">
    
    <file poster="John" date="1227360824" subject="Testing [1/2] - &quot;Test1.zip&quot; yEnc (1/4)">
    <groups><group>alt.binaries.test</group><group>alt.binaries.boneless</group></groups>
    <segments>
    <segment bytes="100000" number="1">Test1</segment>
    <segment bytes="100000" number="2">Test2</segment>
    <segment bytes="100000" number="3">Test3</segment>
    <segment bytes="100000" number="4">Test4</segment>
    </segments>
    </file>
    
    <file poster="John" date="1227360810" subject="Testing [2/2] - &quot;Test2.zip&quot; yEnc (1/2)">
    <groups><group>alt.binaries.test</group><group>alt.binaries.boneless</group></groups>
    <segments>
    <segment bytes="256896" number="1">Test1</segment>
    <segment bytes="140067" number="2">Test2</segment>
    </segments>
    </file>
    
    </nzb>
    I've done half of it, but I'm stuck now. Can somebody please show me how to add these things?

    1) The processing instruction is incorrect. encoding="iso-8859-1" is not added.
    2) The doctype and nzb element need to be added.
    3) Groups/group needs to be added.

    Code:
    <?xml version="1.0"?>
    <file poster="John" date="1227360824" subject="Testing [1/2] - &quot;Test1.zip&quot; yEnc (1/4)">
    <segments>
    <segment bytes="100000" number="1">Test1</segment>
    <segment bytes="100000" number="2">Test2</segment>
    <segment bytes="100000" number="3">Test3</segment>
    </segments>
    </file>
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    Dim objDoc As MSXML2.DOMDocument 'this is a dom document which stores your xml
    Dim objRootElement As IXMLDOMElement ' Create a root xml element
    Dim objCurrentElement As IXMLDOMElement
    Dim objNewElement As IXMLDOMElement
    Dim objProcInst As MSXML2.IXMLDOMProcessingInstruction
    Dim i As Integer
    
        Set objDoc = New DOMDocument
    
        Set objRootElement = objDoc.createElement("file")
        objDoc.appendChild objRootElement
        
        objRootElement.setAttribute "poster", "John"
        objRootElement.setAttribute "date", "1227360824"
        objRootElement.setAttribute "subject", "Testing [1/2] - ""Test1.zip"" yEnc (1/4)"
        
        Set objCurrentElement = objDoc.createElement("segments")
        objRootElement.appendChild objCurrentElement
    
        For i = 1 To 3
            Set objNewElement = objDoc.createElement("segment")
            objCurrentElement.appendChild objNewElement
    
            objNewElement.Text = "Test" & i
    
            objNewElement.setAttribute "bytes", "100000"
            objNewElement.setAttribute "number", i
        Next i
        
        
        Set objProcInst = objDoc.createProcessingInstruction("xml", "version='1.0' encoding='iso-8859-1'")
        objDoc.insertBefore objProcInst, objDoc.childNodes(0)
        
        
        Debug.Print objDoc.xml
        
        Set objDoc = Nothing
        Set objRootElement = Nothing
        Set objCurrentElement = Nothing
        Set objNewElement = Nothing
        Set objProcInst = Nothing
    End Sub

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: Creating an XML file

    I got a bit further.

    1) encoding="iso-8859-1" needs to be added (no idea why it isn't added).
    2) Doctype needs to be added.

    Code:
    <?xml version="1.0"?>
    <nzb xmlns="http://www.newzbin.com/DTD/2003/nzb">
    
    <file poster="John" date="1227360824" subject="Testing [1/2] - &quot;Test1.zip&quot; yEnc (1/4)">
    <groups><group>alt.binaries.test</group></groups>
    <segments>
    <segment bytes="100000" number="1">Test1</segment>
    <segment bytes="100000" number="2">Test2</segment>
    <segment bytes="100000" number="3">Test3</segment>
    </segments>
    </file>
    </nzb>

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    Dim objDoc As MSXML2.DOMDocument 'this is a dom document which stores your xml
    Dim objRootElement As IXMLDOMElement ' Create a root xml element
    Dim objCurrentElement As IXMLDOMElement
    Dim objNewElement As IXMLDOMElement
    Dim objDetails As IXMLDOMElement
    Dim objChild As IXMLDOMElement
    Dim objProcInst As MSXML2.IXMLDOMProcessingInstruction
    Dim i As Integer
    
        Set objDoc = New DOMDocument
    
        Set objProcInst = objDoc.createProcessingInstruction("xml", "version='1.0' encoding='iso-8859-1'")
        objDoc.appendChild objProcInst
    
        
        Set objChild = objDoc.createElement("nzb")
        objDoc.appendChild objChild
        objChild.setAttribute "xmlns", "http://www.newzbin.com/DTD/2003/nzb"
        
        
        Set objRootElement = objDoc.createElement("file")
        objChild.appendChild objRootElement
        
        objRootElement.setAttribute "poster", "John"
        objRootElement.setAttribute "date", "1227360824"
        objRootElement.setAttribute "subject", "Testing [1/2] - ""Test1.zip"" yEnc (1/4)"
        
        Set objCurrentElement = objDoc.createElement("groups")
        objRootElement.appendChild objCurrentElement
        
        Set objDetails = objDoc.createElement("group")
        objDetails.Text = "alt.binaries.test"
        objCurrentElement.appendChild objDetails
        
        
        Set objCurrentElement = objDoc.createElement("segments")
        objRootElement.appendChild objCurrentElement
    
        For i = 1 To 3
            Set objNewElement = objDoc.createElement("segment")
            objCurrentElement.appendChild objNewElement
    
            objNewElement.Text = "Test" & i
    
            objNewElement.setAttribute "bytes", "100000"
            objNewElement.setAttribute "number", i
        Next i
        
        
        Debug.Print objDoc.xml
        
        Set objDoc = Nothing
        Set objRootElement = Nothing
        Set objCurrentElement = Nothing
        Set objNewElement = Nothing
        Set objProcInst = Nothing
        Set objDetails = Nothing
        Set objChild = Nothing
    End Sub

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Creating an XML file

    Code:
    Public Function AddPI(oDOM As DOMDocument, _
                                oPNode As IXMLDOMNode, _
                                sTarget As String, _
                                sInstruction As String _
                                ) As Boolean
        On Error GoTo ErrHand
       
    
        Dim piNode As MSXML2.IXMLDOMProcessingInstruction
        Dim bResults As Boolean
        
        Call ClearErrorInfo
        
        Select Case oPNode.NodeType
            Case NODE_DOCUMENT, NODE_DOCUMENT_FRAGMENT, _
                 NODE_ENTITY_REFERENCE, NODE_ELEMENT:
                Set piNode = oDOM.createProcessingInstruction(sTarget, sInstruction)
                oPNode.appendChild piNode
                bResults = True
            Case Else
                bResults = False
                Call SetErrorInfo(-1, "Invalid parent node type.", _
                    "CDomFunctions.AddPI", HIERARCHY_REQUEST_ERR)
        End Select
        
    ErrHand:
    
        Set piNode = Nothing
        
        If Err.Number <> 0 Then
            bResults = False
            Call SetErrorInfo(Err.Number, Err.Description, "CDomFunctions." _
                & Err.Source, UNKNOWN)
        End If
        
        AddPI = bResults
    End Function

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: Creating an XML file

    Can you please give me an example how to use that function? I've tried several things, but nothing is added.

    I only need these two lines at the top of the xml file and I'm done.

    Code:
    <?xml version="1.0" encoding="iso-8859-1" ?>
    <!DOCTYPE nzb PUBLIC "-//newzBin//DTD NZB 1.0//EN" "http://www.newzbin.com/DTD/nzb/nzb-1.0.dtd">

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