I have an XML file and want to insert from php4.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MiaGreen
    New Member
    • Sep 2010
    • 6

    I have an XML file and want to insert from php4.

    XML:
    Code:
    <shops> 
    <fashion> 
    <shirt> 
    <product> 
    <soort>A</soort> 
    <link>B</link> 
    </product> 
    </shirt> 
    <pants> 
    <product> 
    <soort>E</soort> 
    <link>F</link> 
    </product> 
    </pants> 
    </fashion> 
    </shops>


    PHP4:
    Code:
    $xml = domxml_open_file('shop.xml'); 
    
    $newproduct = $xml->fashion->shirt->create_element("product"); 
    $newproduct->append_child("soort","aaaaa"); 
    $newproduct->append_child("link","bbbbb"); 
    $a = $xml->append_child($newproduct); 
    $xml->dump_file('shop.xml');


    The file is written, but nothing has been changed. Why?
    Last edited by Dormilich; Sep 6 '10, 10:02 PM. Reason: Please use [code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    are you sure this works? this is not SimpleXML.
    Code:
    [B]$newproduct = $xml->fashion->shirt[/B]->create_element("product");

    Comment

    • MiaGreen
      New Member
      • Sep 2010
      • 6

      #3
      So this is where the miswriting starts?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        I’m not too familiar with DOM in PHP 4, but that’s definitely not DOM syntax. this wouldn’t even work in JavaScript.

        Comment

        • MiaGreen
          New Member
          • Sep 2010
          • 6

          #5
          First, thank you for trying to help me.

          Now i also tried this:

          Code:
          $newproduct = $xml->get_elements_by_tagname("shirt");
          and this isn't working also.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            not working in what regard? createElement() is a method of the Document, not of a Node*.

            additionally, if you’r not sure, whether the method call succeeds, print it out with var_dump().

            * - fyi, I’m using the spelling as defined in the specs (which slightly differs from the spelling used for PHP 4).

            Comment

            • MiaGreen
              New Member
              • Sep 2010
              • 6

              #7
              OK, thanks i did not know that. But how do i get further in the tree for inserting the product part?

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                how familiar are you with JavaScript?

                Comment

                • MiaGreen
                  New Member
                  • Sep 2010
                  • 6

                  #9
                  wel, i can write xml with javascript for output to the site and getting al the tree elements. But i dont know how to save the xml in javascript. But i can look it up. You think thats a better solution for this?

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    no, but in JavaScript you work all the time with the DOM. what I wanted to say – it works like in JavaScript.

                    e.g. (see the similarities)
                    Code:
                    // JavaScript
                    var p = document.createElement("p");
                    document.getElementById("test").appendChild(p);
                    Code:
                    // PHP (untested)
                    $p = $xml->create_element("p");
                    $xml->get_element_by_id("test")->append_child($p);

                    Comment

                    • MiaGreen
                      New Member
                      • Sep 2010
                      • 6

                      #11
                      OK! i see the simularity there. Thank you so much i think this is it.

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        that’s the good part of the DOM, it’s used the same on every implementing system.

                        Comment

                        Working...