I posted something similar weeks ago but no answer..so let's try again 🙂
I am using phpDom with dom functions to add/edit/delete nodes in XML. I noticed that the new_child function is writing all the new nodes on ONE LINE in the document. Rather than in Pretty print with newlines. This is ok for deleting and parsing the nodes. But if you try to use the edit functions for phpDom (appendChild) you get errors for nodes all on one line. Something about there not being an object for that function.

But if I go in and edit my xml manually and put carraige returns between the nodes so it's pretty, the edit function of phpDom works again. I'm at a total loss? Does anyone know how to get the new_child function to add newlines in between nodes? AND PLEASE don't say "\n" , i'm not writing strings to a doc..it's class/object oriented so I can't just say "\n" in between adding a child. Thanks!

    i said \n before... and that IS the answer.

    if you want a new line in a 'document' like you said before... you have to put it there, they aren't magic!

    you'll have to edit the add child code or whatever and make it append the \n

      uhh its all object oriented. i really dont see how a '\n' helps you out. thats really strange, it should not make a difference at all. also phpdom does not support node deleting yet(i could be wrong though), so i dont recomend using it just yet.

      but anyways you should not be looking at if there is a carriage return between nodes, but rather, why the freak phpDom cares or not. domxml does not care if there is a carriage return or not.. and neither should phpdom.

        Ok, Michael..apparently I'm too dense to see this..so if you or someone else wouldn't mind adding the /n for me in this function I would really appreciate it:

        function appendChild( $NewChild ) {
        if ( is_string( $NewChild ) ) {
        $classes = get_declared_classes();
        $classname = $NewChild;
        if (! in_array( $classname, $classes ) ) {
        $classname = "Node";
        }
        $WorkObj = new $classname;
        if ( $classname == "Node" ) {
        $WorkObj->Tag( $NewChild );
        }
        }
        elseif ( is_object( $NewChild ) ) {
        if ( strtolower( get_class( $NewChild ) ) == strtolower( "DomNode" ) ) {
        $WorkObj = new Node;
        }
        else {
        $WorkObj = $NewChild;
        }
        }
        if ( (! is_subclass_of( $WorkObj, "Node" )) &&
        (! strtolower( get_class( $NewChild ) ) == strtolower( "Node" ) ) ) {
        user_error( "Node-Element ist vom falschen Typ ..." );
        return false;
        }
        // Check for a DomNode-Object - Create one, if it is not there!
        $this->internal_selfCheck();
        // Is the new child attached to a DomNode?
        if (! $WorkObj->node ) {
        // New node, i can store the value of the node
        $WorkObj->node = $this->node->new_child( $WorkObj->nodeName, $WorkObj->nodevalue );
        // New node, so copy the stored attributes!
        for ( reset( $WorkObj->attribute );
        $k = key( $WorkObj->attribute );
        next( $WorkObj->attribute ) ) {
        $WorkObj->node->setattr( $k, $WorkObj->attribute[$k] );
        }
        }
        else {
        // Already attached to a Node!
        // Create the New-Child of the same type as the new one ...
        $tmpnode = $this->node->new_child( $WorkObj->nodeName, $WorkObj->nodevalue );
        $this->
        internal_cloneNode( $tmpnode, $WorkObj->node, true );
        }
        $WorkObj->node = $this->node->lastchild();
        return $WorkObj;
        }

        Thanks! Tin

          Right Rob...I emailed the guy who developed phpDom himself, finally, He said he thinks his script shouldn't care either. And asked if maybe I had a coding error. But here's the thing..If I go into my XML doc and physically add carriage returns, suddenly I get NO script errors, it runs fine. I did an xml document that had 10 nodes, half were newline and half were same line. It consistenly gave me an error on all the onelines and ran fine on the newlines. I'm stumped and so is PHPdom...maybe it's another freaky PHP doesn't like windows problem, I don't know 🙁

            i also need the code that uses it, the state of the objects at the time you ran it, the output from when you ran it, and the output you want from it.

              I emailed with the man who created phpDom, He said that there is no way to effectively add the newline and that he was sure I didn't need it.

              So I finally figured out what was happening..the first half of my xml was newlined (a test doc I had created) and the second half was all on oneline since I had used dom to add them. I had earlier added an extra get_next_child function in between the ones actually getting the data to allow for the newline whitespace (don't ask me why DOM sees newlines as nodes you have to move through..but it does.) But when it got to the nodes that were on oneline it was trying to move through newlines everyother child which weren't there and so it was failing.

              What I had to do was be consistent and put the whole document, test data too, all on one line and remove those extra next_child calls. As soon as I did that everything cleared up. So as of now, we PHPers have to put up with dom funcitons that add everything on one line! But at least it will read it for now. Thanks for everyones help.
              Tinny

                this really does not seems like a phpDom problem. it seems like this is a domxml problem. you might want to check your version of sablot and upgrade.

                  ohh ooops i dint mean to say sablot i meant libxml. my bad.

                  in any case you have a strange error that should not be occuring. i suggest a simple fix of reinstalling your php domxml libraries, install the latest version while your at it. maybe they'll even have the node deletion function built.

                    Hey Rob..you keep posting to this so I think you might not have seen that I found the answer to the problem , haha. Read my "Nevermind" response in the thread to Michael. Oh, and you CAN delete nodes with dom, SORTOF. You can't completely delete a node but you can clear it's contents totally out by setting it's value to " ". Then I just test for content in the node and if it isn't there I tell my program to skip over it. It isn't pretty but it gets the job done for now. Thanks for your help!
                    Tin

                      Write a Reply...