Generated XML with PHP

Discussion in 'XML' started by Joel Witherspoon, Apr 11, 2004.

  1. I'm generating an xml file using PHP DOMXML . I'm able to create and
    edit the file fine, however the formatting of my file is off. Instead
    of being the standard xml:
    <?xml version="1.0">
    <root>
    <element>
    </element>
    </root>

    I am getting:
    <?xml version="1.0">
    <root>
    <element></element></root>

    Here is the code:

    PHP:
    $tdindex = time(); //creates unix time var for indexing
    $doc = domxml_new_doc('1.0');
    $root = $doc->add_root('categories');
    $category = $doc->create_element('category');
    $category = $root->append_child($category);

    $category->set_attribute('index',$tdindex);
    $cattext = $doc->create_text_node($cat);
    $cattext = $category->append_child($cattext);
    $description =
    $doc->create_element('description');
    $description =
    $category->append_child($description);
    $desctext = $doc->create_text_node($desc);
    $desctext =
    $description->append_child($desctext);



    //create file if it doesn't exist and dump memory into it
    $filename = realpath('categories.xml');
    $doc->dump_file($filename, false, true);


    Is there an method in DOMXML that formats the file? I thought
    dump_file or dump_mem handled it.
    Any help is appreciated.

    __________________
    >>>>>>>>>>>>>>>>>>>>>>>

    Joel
    <<<<<<<<<<<<<<<<<<<<<<<
     
    Joel Witherspoon, Apr 11, 2004
    #1
    1. Advertising

  2. Joel Witherspoon wrote:

    > I'm generating an xml file using PHP DOMXML . I'm able to create and
    > edit the file fine, however the formatting of my file is off. Instead
    > of being the standard xml:
    > <?xml version="1.0">
    > <root>
    > <element>
    > </element>
    > </root>
    >
    > I am getting:
    > <?xml version="1.0">
    > <root>
    > <element></element></root>
    >
    > Here is the code:
    >
    > PHP:
    > $tdindex = time(); //creates unix time var for indexing
    > $doc = domxml_new_doc('1.0');
    > $root = $doc->add_root('categories');


    add_root is deprecated, consider using create_element and then append_child.

    > $category = $doc->create_element('category');
    > $category = $root->append_child($category);
    >
    > $category->set_attribute('index',$tdindex);
    > $cattext = $doc->create_text_node($cat);
    > $cattext = $category->append_child($cattext);
    > $description =
    > $doc->create_element('description');
    > $description =
    > $category->append_child($description);
    > $desctext = $doc->create_text_node($desc);
    > $desctext =
    > $description->append_child($desctext);
    >
    >
    >
    > //create file if it doesn't exist and dump memory into it
    > $filename = realpath('categories.xml');
    > $doc->dump_file($filename, false, true);
    >
    >
    > Is there an method in DOMXML that formats the file? I thought
    > dump_file or dump_mem handled it.



    When I try the following with PHP 4.3.3 on Windows

    <?php
    $xmlDocument = domxml_new_doc('1.0');
    $documentElement = $xmlDocument->create_element('gods');
    $xmlDocument->append_child($documentElement);
    $god = $xmlDocument->create_element('god');
    $name = $xmlDocument->create_element('name');
    $name->append_child($xmlDocument->create_text_node('Kibo'));
    $god->append_child($name);
    $documentElement->append_child($god);
    $xmlDocument->dump_file('test20040411.xml', FALSE, TRUE);
    ?>
    <p>
    <a href="test20040411.xml">test XML file</a>
    </p>

    then the resulting file looks fine to me

    <?xml version="1.0"?>
    <gods>
    <god>
    <name>Kibo</name>
    </god>
    </gods>

    What version of PHP are you using?
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Apr 11, 2004
    #2
    1. Advertising

  3. Martin Honnen <> wrote in message news:<4079205a$>...
    > Joel Witherspoon wrote:
    >
    > > I'm generating an xml file using PHP DOMXML . I'm able to create and
    > > edit the file fine, however the formatting of my file is off. Instead
    > > of being the standard xml:
    > > <?xml version="1.0">
    > > <root>
    > > <element>
    > > </element>
    > > </root>
    > >
    > > I am getting:
    > > <?xml version="1.0">
    > > <root>
    > > <element></element></root>
    > >
    > > Here is the code:
    > >
    > > PHP:
    > > $tdindex = time(); //creates unix time var for indexing
    > > $doc = domxml_new_doc('1.0');
    > > $root = $doc->add_root('categories');

    >
    > add_root is deprecated, consider using create_element and then append_child.
    >
    > > $category = $doc->create_element('category');
    > > $category = $root->append_child($category);
    > >
    > > $category->set_attribute('index',$tdindex);
    > > $cattext = $doc->create_text_node($cat);
    > > $cattext = $category->append_child($cattext);
    > > $description =
    > > $doc->create_element('description');
    > > $description =
    > > $category->append_child($description);
    > > $desctext = $doc->create_text_node($desc);
    > > $desctext =
    > > $description->append_child($desctext);
    > >
    > >
    > >
    > > //create file if it doesn't exist and dump memory into it
    > > $filename = realpath('categories.xml');
    > > $doc->dump_file($filename, false, true);
    > >
    > >
    > > Is there an method in DOMXML that formats the file? I thought
    > > dump_file or dump_mem handled it.

    >
    >
    > When I try the following with PHP 4.3.3 on Windows
    >
    > <?php
    > $xmlDocument = domxml_new_doc('1.0');
    > $documentElement = $xmlDocument->create_element('gods');
    > $xmlDocument->append_child($documentElement);
    > $god = $xmlDocument->create_element('god');
    > $name = $xmlDocument->create_element('name');
    > $name->append_child($xmlDocument->create_text_node('Kibo'));
    > $god->append_child($name);
    > $documentElement->append_child($god);
    > $xmlDocument->dump_file('test20040411.xml', FALSE, TRUE);
    > ?>
    > <p>
    > <a href="test20040411.xml">test XML file</a>
    > </p>
    >
    > then the resulting file looks fine to me
    >
    > <?xml version="1.0"?>
    > <gods>
    > <god>
    > <name>Kibo</name>
    > </god>
    > </gods>
    >
    > What version of PHP are you using?


    I'm using 4.3.2 on Win2K with Apache 2.0.47. Fixed it. The problem was
    $doc->add_root();. Thanks for your time.
     
    Joel Witherspoon, Apr 11, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    29
    Views:
    1,591
    Harlan Messinger
    Apr 14, 2008
  2. Paul Furman
    Replies:
    13
    Views:
    696
    Paul Furman
    Jun 10, 2009
  3. Rajive Narain
    Replies:
    0
    Views:
    1,936
    Rajive Narain
    Sep 18, 2009
  4. mk
    Replies:
    5
    Views:
    146
  5. Replies:
    3
    Views:
    217
    frothpoker
    Sep 4, 2006
Loading...

Share This Page