#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2010
    Posts
    24
    Rep Power
    0

    Xml formatoutput problem


    below is my php file

    PHP Code:
    <?php

        $ItemID 
    rand(100,100000);
        
    $SellerID $_POST['sellerID'];
        
    $ItemName $_POST['name'];
        
    $Price $_POST['price'];
        
    $Quantity $_POST['quantity'];
        
    $Description $_POST['description'];

        
    $XMLDirectory "../../data";
        
    $XMLFile ="goods.xml";
        
        
    chdir($XMLDirectory);


        if(
    file_exists($XMLFile)) {
            
    $doc DomDocument::load($XMLFile);
            
    $doc->formatOutput true;
            
    $goods $doc->documentElement;
        }
        else {
            
    $doc = new DomDocument('1.0');
            
    $doc->formatOutput true;
            
    $goods $doc->createElement('goods');
            
    $goods $doc->appendChild($goods);
        }
        
    $good $doc->createElement('good');
        
    $good $goods->appendChild($good);
        
    $itemID $doc->createElement('itemID');
        
    $itemID $good->appendChild($itemID);
        
    $itemIDvalue $doc->createTextNode($ItemID);
        
    $itemIDvalue $itemID->appendChild($itemIDvalue);
        
    $seller $doc->createElement('sellerID');
        
    $seller $good->appendChild($seller);
        
    $sellervalue $doc->createTextNode($SellerID);
        
    $sellervalue $seller->appendChild($sellervalue);
        
    $itemname $doc->createElement('name');
        
    $itemname $good->appendChild($itemname);
        
    $itemvalue $doc->createTextNode($ItemName);
        
    $itemvalue $itemname->appendChild($itemvalue);
        
    $unitPrice $doc->createElement('price');
        
    $unitPrice $good->appendChild($unitPrice);
        
    $unitPricevalue $doc->createTextNode($Price);
        
    $unitPricevalue $unitPrice->appendChild($unitPricevalue);
        
    $qty $doc->createElement('quantity');
        
    $qty $good->appendChild($qty);
        
    $qtyvalue $doc->createTextNode($Quantity);
        
    $qtyvalue $qty->appendChild($qtyvalue);
        
    $desc $doc->createElement('description');
        
    $desc $good->appendChild($desc);
        
    $descvalue $doc->createTextNode($Description);
        
    $descvalue $desc->appendChild($descvalue);
        
    $onhold $doc->createElement('onhold');
        
    $onhold $good->appendChild($onhold);
        
    $onholdvalue $doc->createTextNode(0);
        
    $onholdvalue $onhold->appendChild($onholdvalue);
        
    $sold $doc->createElement('sold');
        
    $sold $good->appendChild($sold);
        
    $soldvalue $doc->createTextNode(0);
        
    $soldvalue $sold->appendChild($soldvalue);

        
    $doc->save($XMLFile);

        echo 
    "Your item has been listed in the system, and the item number is: <strong>" .$ItemID"</strong>";
    ?>
    when elements are added for the first time in my XML file the output is perfect.
    Example:

    Code:
    <?xml version="1.0"?>
    <goods>
      <good>
        <itemID>50813</itemID>
        <sellerID>110</sellerID>
        <name>jason</name>
        <price>15</price>
        <quantity>1</quantity>
        <description>test</description>
        <onhold>0</onhold>
        <sold>0</sold>
      </good>
    </goods>
    whereas when the XML file is created an i add a new good all the elements are displayed on a single line.
    Example:

    Code:
    <?xml version="1.0"?>
    <goods>
      <good>
        <itemID>50813</itemID>
        <sellerID>110</sellerID>
        <name>jason</name>
        <price>15</price>
        <quantity>1</quantity>
        <description>test</description>
        <onhold>0</onhold>
        <sold>0</sold>
      </good>
    <good><itemID>39805</itemID><sellerID>113</sellerID><name>sonia</name><price>12</price><quantity>11</quantity><description>test1</description><onhold>0</onhold><sold>0</sold></good></goods>
    The formatoutput only works when the XML file is created and first good is added to the file.

    Can anyone help me please???
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2010
    Posts
    24
    Rep Power
    0
    I find the solution. I had to pass LIBXML_NOBLANKS as a parameter when loading my XML file.
    example:

    $doc = DOMDocument::load($XMLFile, LIBXML_NOBLANKS);

IMN logo majestic logo threadwatch logo seochat tools logo