phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


PHP - XML: Filtering XML Nodes by Namespace

Tag: xml, node, namespace   Category: PHP Basic
post: 23 Mar 2008 read: 703


PHP XML Tips - Part 11: Do you want to find only those nodes belonging to a particular namespace? You can look solution like this:

<?php
$xmlString = <<< END
<?xml version="1.0"?>
<data xmlns:home="http://www.mysite.com/xmlns/home" xmlns:pdf="http://www.mysite.com/xmlns/work">
  <home:file>content1.doc</home:file>
  <pdf:file>content2.pdf</pdf:file>
  <pdf:file>content3.pdf</pdf:file>  
  <home:file>content4.txt</home:file>
  <home:file>content5.rtf</home:file>
  <pdf:file>content6.pdf</pdf:file>        
</data>
END;

$xml  = simplexml_load_string($xmlString);

foreach($xml->children("http://www.mysite.com/xmlns/work") as $file){
  echo $file ." <br />";
}
?>

Result like this:


content2.pdf
content3.pdf
content6.pdf 


Series this article:
PHP - XML: Read from String
PHP - XML: Read from a File
PHP - XML: Retrieving Node Values
PHP - XML: Retrieving Attribute Values
PHP - XML: Processing XML
PHP - XML: Creating XML Document
PHP - XML: Adding XML Nodes
PHP - XML: Removing Node
PHP - XML: Replacing Node
PHP - XML: Filtering XML Nodes with XPath
PHP - XML: Filtering XML Nodes by Namespace

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


619
posting