CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2004
    Posts
    5

    How do you add line breaks after adding a node

    I have the following code to save nodes to an existing xml file, but when the file is saved the nodes are saved all on the same line:

    <type><num>1</num><address>Adf</address></type>

    instead of in a tree format:
    <type>
    <num>1</num>
    <address>Adf</address>
    </type>
    Does anyone know how to add line breaks when adding new nodes?? My Code is below:

    ' Load the required xml file
    Set xmlDoc=Server.CreateObject("MSXML2.FreeThreadedDOMDocument.3.0")
    xmlDoc.async="false"
    xmlDoc.load(Server.MapPath("Propertie.xml"))

    ' Get the current root
    Set nodeList = xmlDoc.getElementsByTagName("propertytolet")
    if nodeList.length > 0 then
    Set parentNode = nodeList(0)

    ' Create the required nodes
    Set typeNode = xmlDoc.createElement("type")
    Set noofbedroomsNode = xmlDoc.createElement("noofbedrooms")
    Set addressNode = xmlDoc.createElement("address")
    Set postcodeNode = xmlDoc.createElement("postcode")
    Set areaNode = xmlDoc.createElement("area")

    ' Assign the variables, which we have retrieved in
    'step 2 to the xml variables
    var_noofbedrooms = Request.Form("noofbedrooms")
    var_address = Request.Form("address")
    var_postcode = Request.Form("postcode")
    var_area = Request.Form("area")

    noofbedroomsNode.text = var_noofbedrooms
    addressNode.text = var_address
    postcodeNode.text= var_postcode
    areaNode.text = var_area

    'Two steps left! First, we'll append the created nodes to the parent node. This can be done with the function parentNode.appendChild("personNode");


    parentNode.appendChild(typeNode)
    typeNode.appendChild(noofbedroomsNode)
    typeNode.appendChild(addressNode)
    typeNode.appendChild(postcodeNode)
    typeNode.appendChild(areaNode)


    ' And finally, we'll save the nodes to the XML file, using the function xmlDoc.save(Server.MapPath("Person.xml"));

    ' 7) Now save the nodes to the file
    xmlDoc.save(Server.MapPath("Propertie.xml"))
    else
    response.Write("Nodelist Empty")
    end if

  2. #2
    Join Date
    May 2003
    Location
    Denmark
    Posts
    1,315
    A line break is just a text node that contains a newline character.

    I'm a bit rusty on DOM but I would think something like
    Code:
    typeNode.appendChild(noofbedroomsNode)
    typeNode.appendChild(createTextNode("\n"))
    typeNode.appendChild(addressNode)
    should create a newline between the num and address node.

    As an alternative to adding the newline characters in DOM, which is a bit cumbersome, I might suggest that you run your xml document through a prettyprinting stylesheet after creating it. Such an xsl stylesheet should be relativly easy to find using Google, or if you are up to it, create your own.
    The biggest problem encountered while trying to design a system that was completely foolproof,
    was, that people tended to underestimate the ingenuity of complete fools.
    Douglas Adams

  3. #3
    Join Date
    Jun 2004
    Posts
    5

    Solution to adding line breaks after adding node to xml file

    Cheers KHP I looked into the createTextNode a little more and came up with the following solution. I was getting the values from a form and writing the below code in vbscript. I had to create a new node for every time i wanted to create a new line but worked fine. Thanks

    V.
    Download AppendXmlFile.zip which contains all of the following code at www.dmsproperty.co.uk/Code/Downloads.asp


    ' Load the required xml file
    Set xmlDoc=Server.CreateObject("MSXML2.FreeThreadedDOMDocument.3.0")
    xmlDoc.async="false"
    xmlDoc.load(Server.MapPath("Propertie.xml"))

    ' Get the current root
    Set nodeList = xmlDoc.getElementsByTagName("propertytolet")
    if nodeList.length > 0 then
    Set parentNode = nodeList(0)

    ' Create the required nodes
    Set typeNode = xmlDoc.createElement("type")
    Set noofbedroomsNode = xmlDoc.createElement("noofbedrooms")
    Set addressNode = xmlDoc.createElement("address")
    Set postcodeNode = xmlDoc.createElement("postcode")
    Set areaNode = xmlDoc.createElement("area")

    ' Create the required Blank text nodes, 3 For the Child, 1 for the element and 1 for the root node
    Set childBlankNode1 = xmlDoc.createTextNode("childBlank1")
    Set childBlankNode2 = xmlDoc.createTextNode("childBlank2")
    Set childBlankNode3 = xmlDoc.createTextNode("childBlank3")

    Set elementBlankNode = xmlDoc.createTextNode("elementBlank")
    Set rootBlankNode = xmlDoc.createTextNode("rootBlank")

    ' Assign the variables from the web form to the xml variables
    var_noofbedrooms = Request.Form("noofbedrooms")
    var_address = Request.Form("address")
    var_postcode = Request.Form("postcode")
    var_area = Request.Form("area")

    noofbedroomsNode.text = var_noofbedrooms
    addressNode.text = var_address
    postcodeNode.text= var_postcode
    areaNode.text = var_area

    ' assign the values for the text nodes childBlankNode1.text = vbCrLf & " "
    childBlankNode2.text = vbCrLf & " "
    childBlankNode3.text = vbCrLf & " "
    childBlankNode4.text = vbCrLf & " "

    elementBlankNode.text = vbCrLf & " "
    rootBlankNode.text = vbCrLf

    'Two steps left! First, we'll append the created nodes to the parent node. This can be done with the function parentNode.appendChild("typeNode");
    parentNode.appendChild(typeNode)

    'then we append the child nodes
    'here we append a child node (noofbedrooms) with a new line and 8 spaces across typeNode.appendChild(childBlankNode1)

    typeNode.appendChild(noofbedroomsNode)

    'the same again is done here for child node (address) typeNode.appendChild(childBlankNode2)

    typeNode.appendChild(addressNode)

    ' and again here for child node (postcode)
    typeNode.appendChild(childBlankNode3)

    typeNode.appendChild(postcodeNode)

    'now we append the element node(type) with a new line and 4 spaces across
    typeNode.appendChild(elementBlankNode)


    'now we append the root node (</propertytolet>) by creating a new line with no spaces parentNode.appendChild(rootBlankNode)

    ' And finally, we'll save the nodes to the XML file, using the function xmlDoc.save(Server.MapPath("Person.xml"));

    ' Now save the nodes to the file
    xmlDoc.save(Server.MapPath("Propertie.xml"))
    else
    response.Write("Nodelist Empty")
    end if
    Last edited by Vender; June 17th, 2004 at 04:28 AM.

  4. #4
    Join Date
    Feb 2017
    Posts
    1

    Resolved Re: How do you add line breaks after adding a node

    This Microsoft article includes the perfect code snippet to do it !
    It is a very short and fully functional way to properly format an XML file under VbScript or Classip ASP.
    It will save your day as it saved mine !
    https://blogs.msdn.microsoft.com/rob...-and-vbscript/

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