How to use registerXPathNamespace in PHP

In this article I will explain how registerXPathNamespace function used in PHP.
  • 3193

registerXPathNamespace() Function

This function is helpful if a namespace prefix is changed in an xml file and create a prefix for specified namespace, so that the affected XML nodes can be accessed without modify the application code too much.

Syntax

class SimpleXMLElement
{
string registerXPathNamespace(prefix,ns)
}


Example

XML File

<?xml version="1.0" encoding="ISO-8859-1"?>
<note xmlns:b="https://dotnetheaven.com/articles/">
<book>iPhone</book>
<writer>wei sang lee</writer>
<name>Reminder</name>
<b:body>Hello, Dinesh sir Ji.</b:body>
</note>

PHP Code

<?php
$xml = simplexml_load_file("parse.xml");

$xml->registerXPathNamespace("msg",
"https://dotnetheaven.com/articles/");
$result = $xml->xpath("msg:body");

foreach ($result as $message)
{
echo $message;
}
?>

Output

 registerXPathnamespace-php.jpg

You may also want to read these related articles Click here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.