Results 1 to 7 of 7

Thread: XML with PHP

  1. #1
    Join Date
    Nov 2007
    Location
    USA
    Posts
    170
    Thanks
    8
    Thanked 22 Times in 22 Posts

    Default XML with PHP

    Hello everyone,

    I am wondering if there is some way to open an XML document, make changes to it and then save the changes. I specifically want to know if there is a way that I can add new elements to the document. I have searched the net for this and have not been able to come up with anything so now I'm asking the pros. Thanks in advance!

    Mosh
    What is obvious to you might not be to another.


    My Site

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    I don't know if there is an xml function you can use... But you can try making your own with:

    file_put_contents & file_get_contents
    fgetcsv
    fwrite

    Maybe some of those.
    Jeremy | jfein.net

  3. #3
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    The SimpleXML object is the quickest way to go about this. Post again if you need help with it.

  4. #4
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Quote Originally Posted by Moshambi View Post
    Hello everyone,
    I'm asking the pros. Thanks in advance!

    Mosh
    I'm not a pro, but

    http://www.php.net/manual/en/book.simplexml.php

    Is fairly straight forward. I played with it for a couple hours and then felt fairly comfortable.

    Didn't see Tim's post. Sry.
    Last edited by JasonDFR; 02-10-2009 at 12:30 PM. Reason: Didn't see Tim's post. Sry.

  5. #5
    Join Date
    Nov 2007
    Location
    USA
    Posts
    170
    Thanks
    8
    Thanked 22 Times in 22 Posts

    Default

    Ok ya I had been looking at the simpleXMLElement stuff before I posted this but I still can't figure out a way to add an element to the xml document then save it. For example:

    XML Doc before I run Code:
    Code:
    <?xml version="1.0"?>
    <body>
    	<element>text</element>
    </body>
    XML Doc after Code:
    Code:
    <?xml version="1.0"?>
    <body>
    	<element>text</element>
    	<element>text</element>
    </body>
    Does this make any sense? I tried using the addChild() function but I don't know how to get it to save itself. I tried $variable->saveXML();

    It will output it but it won't actually save it to the document?

    Thanks for the help,

    Mosh
    What is obvious to you might not be to another.


    My Site

  6. #6
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    Code:
    <?xml version="1.0"?>
    <body>
    	<element>text</element>
    </body>
    PHP Code:
    $simpleXML = new SimpleXMLElement('path/to/xmlfile.xml'0true);
    $simpleXML->addChild('element2''innerText');
    $simpleXML->asXML('path/to/xmlfile.xml'); 
    Code:
    <?xml version="1.0"?>
    <body>
    	<element>text</element>
           <element2>innerText</element2>
    </body>

  7. The Following User Says Thank You to techietim For This Useful Post:

    Moshambi (02-11-2009)

  8. #7
    Join Date
    Nov 2007
    Location
    USA
    Posts
    170
    Thanks
    8
    Thanked 22 Times in 22 Posts

    Default

    AWESOME! That was exactly what I was looking for. Thank you very much!
    What is obvious to you might not be to another.


    My Site

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •