note 95993 added to domdocument.getelementsbytagname
| From: | james at osu1 dot php dot net | Date: | Tue, 02 Feb 2010 03:03:00 +0000 |
| Subject: | note 95993 added to domdocument.getelementsbytagname | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-165583@lists.php.net to get a copy of this message | ||
Problem:
You have an XML document that contains filename references to, say, images. Each filename reference
is defined by <file>filename.ext</file> tag. You'd like to perform perform
additional validation, say, after running the XML document through XSD validation. The additional
validation can be any of your choice, in this example, it would be ideal to convert the PHP code to
a function. The function would then determine if the images exist and return either an integer value
or a boolean.
<?xml version="1.0"?>
<root>
<box>
<file>example.png</file>
</box>
<content>
<item>
<image><file>example2.png</file></image>
<caption>The above image is an example</caption>
</item>
</content>
</root>
Solution:
<?php
$dom = new DomDocument();
$dom->prevservWhiteSpace = false;
if (!@$dom->load("example.xml")) {
echo "example.xml doesn't exist!\n";
return;
}
$imageList = $dom->getElementsByTagName('file');
$imageCnt = $imageList->length;
for ($idx = 0; $idx < $imageCnt; $idx++) {
print $imageList->item($idx)->nodeValue . "\n";
}
?>
The above PHP code could easily be turned into a function that returns an array of image filenames,
an integer value relative to the number of images found, etc.
Hope this is helpful.
----
Server IP: 192.41.40.227
Probable Submitter: 98.231.164.224
----
Manual Page -- http://www.php.net/manual/en/domdocument.getelementsbytagname.php
Edit -- https://master.php.net/note/edit/95993
Del: integrated -- https://master.php.net/note/delete/95993/integrated
Del: useless -- https://master.php.net/note/delete/95993/useless
Del: bad code -- https://master.php.net/note/delete/95993/bad+code
Del: spam -- https://master.php.net/note/delete/95993/spam
Del: non-english -- https://master.php.net/note/delete/95993/non-english
Del: in docs -- https://master.php.net/note/delete/95993/in+docs
Del: other reasons-- https://master.php.net/note/delete/95993
Reject -- https://master.php.net/note/reject/95993
Search -- https://master.php.net/manage/user-notes.php