DOM formatOutput issues

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • segfalt@gmail.com

    DOM formatOutput issues

    Hey All, I'm wondering if someone can give me a hand with the
    following.

    I'm frequently updating xml files, sometimes in bulk updates. I'm
    having trouble getting the output formatted nicely (not one long line).
    Here is an example.

    I would create the doc using something like the following code:

    $dom = new DOMDocument("1. 0","iso-8859-1");
    $dom->formatOutput = true;
    $rootnode = $dom->createElement( "users");
    $id = $dom->createElement( "user_id", "0");
    $user = $dom->createElement( "user_name" , "Mark_0");
    $rootnode->appendChild($i d);
    $rootnode->appendChild($u ser);
    $dom->appendChild($r ootnode);
    $dom->save('domfile. xml');


    Later, when I add information to the xml file, I'd use something like
    this:

    $dom = new DOMDocument();
    $dom->formatOutput = true;

    $dom->load('domfile. xml');

    $rootnode = $dom->getElementsByT agName("users")->item(0);
    for($x=1;$x<=10 00;$x++) {
    $id = $dom->createElement( "user_id", "{$x}");
    $user = $dom->createElement( "user_name" , "Mark_{$x}" );

    $rootnode->appendChild($u ser);
    $rootnode->appendChild($i d);
    }
    $dom->appendChild($r ootnode);
    $dom->save('domfile. xml');

    The data get's in there but the xml file looks like this:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <users>
    <user_id>0</user_id>
    <user_name>Mark _0</user_name>
    <user_name>Mark _1</user_name><user _id>1</user_id><user_n ame>Mark_2</user_name><user _id>2</user_id><user_n ame>Mark_3</user_name><user _id>3</user_id>....... </users>

    Everything appended to the document will be added on the same line as
    the last entry. I'd like it to contine on appending new lines for
    elements but it doesn't appear to be happening.

    Anyone know what I'm doing wrong or is this just 'the way it is'.

    Thanks
    Mark

  • bobzimuta

    #2
    Re: DOM formatOutput issues

    Tried some variations myself, but no luck. However, I question the need
    to view the XML file in a pretty fashion, since XML files aren't meant
    for viewing. And if you do view it, view in a web browser that can
    display the tree structure nicely.

    Also, I wonder why you are constructing the DOM tree the way you are.
    The way you have it now:

    <users>
    <user_id>0</user_id>
    <user_name>Mark _0</user_name>
    <user_id>1</user_id>
    <user_name>Mark _1</user_name>
    </users>

    Wouldn't you want to seperate out the users into their own nodes? As
    in:

    <users>
    <user>
    <user_id>0</user_id>
    <user_name>Mark _0</user_name>
    </user>
    <user>
    <user_id>1</user_id>
    <user_name>Mark _1</user_name>
    </user>
    </users>

    Comment

    • bobzimuta

      #3
      Re: DOM formatOutput issues

      I realize now that this was just your test data. But I still question
      its structure ;-)

      Comment

      • segfalt@gmail.com

        #4
        Re: DOM formatOutput issues

        Yah, this actually isn't the schema I'm using at all. It was purely a
        test case to generate the output example. The real schemas I'm using
        are a fair amount more complex and yes I do split objects into their
        own nodes in those.

        I'd prefer the output be formatted nicely, really, only for debugging
        and the odd hand fix purposes. Every once and a while it's nice to be
        able to vi the xml and fix something by hand (especially if it was the
        php which generated the bad xml). Trying to modify or spot errors in a
        2 - 5MB file in vi (or anything for that matter) with no new line
        characters is next to impossible. What I don't get is why the first
        entry comes out formatted but not the subsequent entries.

        Thanks
        Mark

        Comment

        • bobzimuta

          #5
          Re: DOM formatOutput issues

          After thinking about it I think I may know why. The formatting works
          when writing the intial XML document since it's easy enough for the
          write method to know how far into the tree it is, and add the requisite
          number of tabs or spaces. But to append the new nodes, it would have to
          do some more complicated logic to find out how many tabs/spaces in the
          tree the new nodes will be, and go from there. Likely it was lazy
          and/or time restricted PHP programmers.

          If this is the case, one workaround may be to read in the XML file,
          append the nodes, then replace the XML file by creating a new one.
          You'll get the nicely formatted tree, though sacrificing speed.

          Comment

          • segfalt@gmail.com

            #6
            Re: DOM formatOutput issues

            Hrm, yes interesting point. It's probably not worth it. I can just
            use an xml inspector to view/modify by hand if needed. Thanks for the
            help though.

            Mark

            Comment

            Working...