Following Example is of multiple attributes and multiple child nodes. this is being used to make joomla plugin for bulk upload of articles. Gurmukh Singh Bhatti
<?php
$xml =<<<EOT
<?xml version="1.0"?>
<root>
<section name="Section1">
<category id="Category1" name="google">
<arti name="article1">
<p>any html code here</p>
<b>my name is so so</b>
</arti>
<arti name="article2">value2</arti>
<arti name="article3">value3</arti>
<arti name="article4">value4</arti>
</category>
<category id="Category2" name="yahoo">
<arti name="articleSection2">Test value</arti>
</category>
</section>
<section name="Section2">
<category id="category1_of_section2" name="msn">
<arti name="article2">value1</arti>
<arti name="article3">value2</arti>
</category>
<category id="Category2_of_section2" name="webcare">
<arti name="param3">value4</arti>
</category>
</section>
</root>
EOT;
$dom = new DomDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xml);
$params = $dom->getElementsByTagName('section'); // Find Sections
$k=0;
foreach ($params as $param) //go to each section 1 by 1
{
echo "Section Attribute :-> ".$params->item($k)->getAttribute('name')."<br>"; //get section attribute
$params2 = $params->item($k)->getElementsByTagName('category'); //digg categories with in Section
$i=0; // values is used to iterate categories
foreach ($params2 as $p) {
echo " - Category Attribute Name :-> ".$params2->item($i)->getAttribute('name')."<br>"; //get Category attributes
$params3 = $params2->item($i)->getElementsByTagName('arti'); //dig Arti into Categories
$j=0;//values used to interate Arti
foreach ($params3 as $p2)
{
echo " - Article Attribute Name : ".$params3->item($j)->getAttribute('name').""; //get arti atributes
echo " Value : ".$params3->item($j)->nodeValue."<br>"; //get Node value ;
$j++;
}
$i++;
}
$k++;
}
?>
output :
Section Attribute :-> Section1
- Category Attribute Name :-> google
- Article Attribute Name : article1 Value : any html code heremy name is so so
- Article Attribute Name : article2 Value : value2
- Article Attribute Name : article3 Value : value3
- Article Attribute Name : article4 Value : value4
- Category Attribute Name :-> yahoo
- Article Attribute Name : articleSection2 Value : Test value
Section Attribute :-> Section2
- Category Attribute Name :-> msn
- Article Attribute Name : article2 Value : value1
- Article Attribute Name : article3 Value : value2
- Category Attribute Name :-> webcare
- Article Attribute Name : param3 Value : value4
DOMDocument::getElementsByTagName
(PHP 5)
DOMDocument::getElementsByTagName — Cherche tous les éléments qui ont le nom de balise donné
Description
Cette fonction retourne une instance de la classe DOMNodeList contenant les éléments qui ont un nom de balise donné.
Liste de paramètres
- name
-
Le nom de la balise à chercher. La valeur spéciale * correspond à toutes les balises.
Valeurs de retour
Un nouvel objet DOMNodeList contenant tous les éléments correspondants.
Voir aussi
- DOMDocument::getElementsByTagNameNS - Recherche tous les éléments avec un nom de balise donné dans un espace de noms spécifié
DOMDocument::getElementsByTagName
gurmukh24 at gmail dot com
03-Mar-2009 12:06
03-Mar-2009 12:06
StudioAMK.com
10-Oct-2008 03:09
10-Oct-2008 03:09
<?php
$doc = new DOMDocument();
$doc->load( 'Users.xml' );
$dataset = $doc->getElementsByTagName( "dataUser" );
foreach( $dataset as $row )
{
$xmlUserNames = $row->getElementsByTagName( "UserName" );
$xmlUserName = $xmlUserNames->item(0)->nodeValue;
$xmlEmails = $row->getElementsByTagName( "Email" );
$xmlEmail = $xmlEmails->item(0)->nodeValue;
$xmlDisplayNames = $row->getElementsByTagName( "DisplayName" );
$xmlDisplayName = $xmlDisplayNames->item(0)->nodeValue;
echo "$xmlUserName - $xmlEmail - $xmlDisplayName\n";
}
?>
Contents in Users.xml
<NewDataSet>
<dataUser>
<UserName>StudioAMK</UserName>
<Email>user1@mail.com</Email>
<DisplayName>StudioAMK.com</DisplayName>
</dataUser>
<dataUser>
<UserName>User2</UserName>
<Email>user2@mail.com</Email>
<DisplayName>UserTwo</DisplayName>
</dataUser>
</NewDataSet>
James L
19-Aug-2008 11:04
19-Aug-2008 11:04
Return if there are no matches is an empty DOMNodeList. Check using length property, e.g.:
<?php
$nodes=$domDocument->getElementsByTagName('book') ;
if ($nodes->length==0) {
// no results
}
?>
jason at shaped dot ca
11-Feb-2008 12:59
11-Feb-2008 12:59
in response to tildy at pr dot hu
my preferred way is (in example to gather country data from an iso 3166 xml flie)
$countries = new DOMDocument();
$countries->load("./lib/iso_3166.xml");
$countriesList = $countries->getElementsByTagName("ISO_3166-1_Entry");
foreach($countriesList as $country) {
$values = $country->getElementsByTagName("*");
foreach($values as $node) {
echo $node->nodeName."=".$node->nodeValue;
}
}
tildy at pr dot hu
13-Dec-2007 12:51
13-Dec-2007 12:51
If you want to list the nodename and value of one item(node) this is an example:
$itemnodes = $doc->getElementsByTagName( "item" );
$nodes = $itemnodes->item(0)->getElementsByTagName( "*" );
for ( $i = 0; $i < $nodes->length; $i++ ) {
print "nodename=".$nodes->item( $i )->nodeName;
print "\t";
print "nodevalue : ".$nodes->item( $i )->nodeValue;
print "\r\n";
}
It will be list all children name and value of item.
Francois Hill
30-Jul-2007 02:36
30-Jul-2007 02:36
Careful : getElementsByTagName will yield all elements with the given tag name under the present node, at any sub-level (i.e. among child nodes and all other descendant nodes)
Finding values of a node
14-Mar-2007 10:01
14-Mar-2007 10:01
I don't know if this is that obvious but it was not for me, so in addition to gem at rellim dot com's posting:
adding
<?php
echo $param -> nodeValue.'<br>';
?>
to the loop will output
value1
value2
value3
gem at rellim dot com
29-Sep-2004 11:20
29-Sep-2004 11:20
Here is an example of getElementsByTagName():
<?php
$xml =<<<EOT
<?xml version="1.0"?>
<config>
<section id="section1">
<param name="param1">value1</param>
<param name="param2">value2</param>
</section>
<section id="section2">
<param name="param3">value3</param>
</section>
</config>
EOT;
$dom = new DomDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xml);
$params = $dom->getElementsByTagName('param');
foreach ($params as $param) {
echo $param -> getAttribute('name').'<br>';
}
?>
Expected result:
--------------
param1
param2
param3
