A problem with a DOM representation...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Pesho
Forum Newbie
Posts: 12
Joined: Fri Jan 27, 2006 9:43 am

A problem with a DOM representation...

Post by Pesho »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hi all,

I guess my problem is really stupid, but I just can't figure it out.
I have to extract information from a DOM representation. My XML content is:

[syntax="xml"]
<record>
      <header>
        <identifier>oai:ub.rug.nl:dbi/4357aa6e091c0</identifier>
      </header>
      <metadata>
        <oai_dc:dc xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/            http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
          <dc:title>Just take her seriously</dc:title>
          <dc:creator>Weijmar Schultz, W.C.M.</dc:creator>
        </oai_dc:dc>
      </metadata>
</record>
first I try to extract information about the <metadata> tag:[/syntax]

Code: Select all

$records = $dom->documentElement->getElementsByTagName('metadata');
foreach($records as $record) {
	$children = $record->childNodes;
	foreach($children as $child) {
		echo $child->nodeName;
	}
}
And it works as expected. The output is: #textoai_dc:dc#text

However, when I try the same thing for the <dc:creator> tag, the otput is EMPTY.....

The following code:

Code: Select all

$records = $dom->documentElement->getElementsByTagName('dc:creator');
foreach($records as $record) {
	echo $record->nodeName;
}
also gives an empty output.


I'm totally confused why it doesn't return any information about the <dc:creator> tag.


Thanks in advance Smiley
Pesho


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Works fine for me

Code: Select all

<?php
$xml = <<< eox
<record>
      <header>
        <identifier>oai:ub.rug.nl:dbi/4357aa6e091c0</identifier>
      </header>
      <metadata>
        <oai_dc:dc xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/            http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
          <dc:title>Just take her seriously</dc:title>
          <dc:creator>Weijmar Schultz, W.C.M.</dc:creator>
        </oai_dc:dc>
      </metadata>
</record>
eox;

echo phpversion(), "\n";

$dom = DOMDocument::loadxml($xml);
//$records = $dom->documentElement->getElementsByTagNameNS('http://purl.org/dc/elements/1.1/', 'creator');
$records = $dom->documentElement->getElementsByTagName('creator');
foreach($records as $record) {
   echo '. ', $record->nodeName;
}
prints
5.2.1
. dc:creator
Post Reply