DOM in PHP

P

Paul Stuck

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
 
J

Joe Kesselman

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.)
 
P

Paul Stuck

Joe said:
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
 
J

Joe Kesselman

Paul said:
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.
 
J

Joe Kesselman

Harrie said:
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

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.
 
P

Paul Stuck

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top