DOM in PHP

Discussion in 'XML' started by Paul Stuck, Feb 6, 2006.

  1. Paul Stuck

    Paul Stuck Guest

    hi there

    i've got a problem by inserting a new node into a document.

    this is the xml file:

    <?xml version="1.0"?>
    <satz>
    <id>1</id>
    <data></data>
    <info>some info</info>
    </satz>

    i want to insert a new tag <info> filled with text from user input.
    so i tried with this code:

    $doc = new DOMDocument();
    $doc->load('daten.xml');

    $neu = $_POST['neu'];

    $satz = $doc->getElementsByTagName( "satz" );

    $r = $doc->createElement( "info", $neu );
    $doc->appendChild( $r);

    $doc->save('filename.xml');

    echo $doc->saveXML();


    but this codes adds the <info> tag at the end of the document. how can i insert
    the element in the right place?

    greets

    paul
     
    Paul Stuck, Feb 6, 2006
    #1
    1. Advertising

  2. Did you intend to append the new element to $satz rather than $doc?

    If you need more control than that, you probably want the insertBefore
    operation rather than append.

    (There are many good tutorials on how to use the DOM. Reading one, or at
    the very least taking the time to read thru the DOM spec to see what
    operations are available, is going to be a lot more effective than
    trying to guess.)
     
    Joe Kesselman, Feb 7, 2006
    #2
    1. Advertising

  3. Paul Stuck

    Paul Stuck Guest

    Joe Kesselman schrieb:
    > Did you intend to append the new element to $satz rather than $doc?
    >
    > If you need more control than that, you probably want the insertBefore
    > operation rather than append.

    okay, looks nice.


    > (There are many good tutorials on how to use the DOM.

    didn't found one. can you tell me an url?

    thanks

    greetings

    paul
     
    Paul Stuck, Feb 7, 2006
    #3
  4. Paul Stuck wrote:
    >>(There are many good tutorials on how to use the DOM.

    > didn't found one. can you tell me an url?


    I don't know of any specifically with regard to PHP, but there are
    certainly lots of articles on using the DOM with Java and other
    languages, and the APIs and their behavior *should* be essentially the
    same modulo any differences absolutely required by the language.

    When I'm looking for training material, I usually start with IBM's
    DeveloperWorks website at http://ibm.com/xml -- OK, I admit I'm biased,
    but this is still a good starting point for standards education. If you
    click on Training on the left side, then select Tutorials, then select
    DOM from the drop-down list, you'll see 9 tutorials at various levels of
    complexity and specificity. If you go back up to the top and change
    "tutorials" to "articles", you'll see another 35 relevant documents;
    there are also a few in the "standards" category.
     
    Joe Kesselman, Feb 8, 2006
    #4
  5. Paul Stuck

    Paul Stuck Guest

    Joe Kesselman schrieb:
    > When I'm looking for training material,

    okay, thank you, i usually looked at the w3schools page...


    greets

    paul
     
    Paul Stuck, Feb 8, 2006
    #5
  6. Paul Stuck

    Harrie Guest

    Paul Stuck said the following on 2/8/2006 21:40 +0200:
    > Joe Kesselman schrieb:
    >
    >>When I'm looking for training material,

    >
    > okay, thank you, i usually looked at the w3schools page...


    I mostly look at the W3C's website, although I find their pages harder
    to digest. Here's a page with all DOM levels:

    http://www.w3.org/DOM/DOMTR

    And a Google search on "PHP DOM" found this:

    http://www.php.net/domxml/

    --
    Regards
    Harrie
     
    Harrie, Feb 10, 2006
    #6
  7. Harrie wrote:
    > I mostly look at the W3C's website, although I find their pages harder
    > to digest.


    The official specs are written by experts for experts, and unfortunately
    companies don't donate editorial expertise, so they can be a bit of an
    uphill slog. But they're the authoritative source of info for anything
    developed by the W3C, and are definitely worth knowing about and
    checking when in doubt.

    My usual starting point there is
    > http://www.w3.org/TR


    which is a list of all the technical reports they've published,
    including all the Recommendations already published and the Working
    Drafts still being developed.
     
    Joe Kesselman, Feb 10, 2006
    #7
  8. Paul Stuck

    Paul Stuck Guest

    Harrie schrieb:
    > Paul Stuck said the following on 2/8/2006 21:40 +0200:
    >> Joe Kesselman schrieb:
    >>
    >>> When I'm looking for training material,

    >>
    >> okay, thank you, i usually looked at the w3schools page...

    >
    > I mostly look at the W3C's website, although I find their pages harder
    > to digest. Here's a page with all DOM levels:
    >
    > http://www.w3.org/DOM/DOMTR

    yep, but i meant this page: http://www.w3schools.com/



    >
    > And a Google search on "PHP DOM" found this:
    >
    > http://www.php.net/domxml/

    yeah, look there already but it's not explained in detail what each function and
    method does.

    greets

    paul
     
    Paul Stuck, Feb 10, 2006
    #8
  9. Paul Stuck wrote:


    >>And a Google search on "PHP DOM" found this:
    >>
    >> http://www.php.net/domxml/

    >
    > yeah, look there already but it's not explained in detail what each function and
    > method does.


    And note that for PHP 5 and its built-in DOM support you need
    <http://www.php.net/manual/en/ref.dom.php>
    The domxml extension is for PHP 4.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Feb 10, 2006
    #9
    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. Ted Zeng
    Replies:
    4
    Views:
    564
    Tim Arnold
    Oct 3, 2006
  2. Ted Zeng
    Replies:
    0
    Views:
    679
    Ted Zeng
    Oct 13, 2006
  3. torque63
    Replies:
    0
    Views:
    659
    torque63
    Jan 1, 2009
  4. raviraj joshi
    Replies:
    0
    Views:
    357
    raviraj joshi
    Jul 4, 2009
  5. Rajive Narain
    Replies:
    0
    Views:
    2,066
    Rajive Narain
    Sep 18, 2009
Loading...

Share This Page