Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate namespace prefixed xml elements using SimpleXMLElement in PHP

Tags:

php

xml

I'm trying to generate a RSS feed using PHP SimpleXMLElement, the problem is that i need to prefix elements and can't find a way to do this using the SimpleXMLElement class.

I've tried using $item->addChild('prefix:element', 'value') but in the result xml it strips the prefix, any idea why this happens ?.

I wonder if there is a way to solve this using the SimpleXMLElement or any other cleaner way than just echoing the XML.

For clarification, this is my PHP code:

    $xml = new SimpleXMLElement('<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0"/>');
    $channel = $xml->addChild('channel');
    $channel->addChild('title', 'Text');
    $channel->addChild('link', 'http://example.com');
    $channel->addChild('description', 'An example item from the feed.');

    foreach($this->products as $product) {
        $item = $channel->addChild('item');

        foreach($product as $key => $value)
            $item->addChild($key, $value);
    }

    return $xml->asXML();

And this is the example XML i'm trying to generate:

<?xml version="1.0"?>
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
    <title>Test Store</title>
    <link>http://www.example.com</link>
    <description>An example item from the feed</description>

    <item>
        <g:id>DB_1</g:id>
        <g:title>Dog Bowl In Blue</g:title>
        <g:description>Solid plastic Dog Bowl in marine blue color</g:description>
        ...
    </item>
...

Thanks in advance

like image 829
Nodiel Clavijo Llera Avatar asked Dec 04 '15 19:12

Nodiel Clavijo Llera


People also ask

What is namespace prefix in XML?

When using prefixes in XML, a namespace for the prefix must be defined. The namespace can be defined by an xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix="URI".

What is XPath namespace?

XPath queries are aware of namespaces in an XML document and can use namespace prefixes to qualify element and attribute names. Qualifying element and attribute names with a namespace prefix limits the nodes returned by an XPath query to only those nodes that belong to a specific namespace.

What is XML schema namespace?

Figure 1: Elements and attributes in XML Schema namespace are used to write an XML Schema document, which generates elements and attributes as defined by user and puts them in {target namespace}. This {target namespace} is then used to validate the XML instance.

Can XML have multiple namespaces?

When you use multiple namespaces in an XML document, you can define one namespace as the default namespace to create a cleaner looking document. The default namespace is declared in the root element and applies to all unqualified elements in the document. Default namespaces apply to elements only, not to attributes.

What is the use of the simplexmlelement attribute in PHP?

The SimpleXMLElement::attributes() function is an inbuilt function in PHP which is used to retrieve the attributes and its value from an XML tag in a SimpleXML object.

How to retrieve the value of an XML tag in SimpleXML?

The SimpleXMLElement::attributes () function is an inbuilt function in PHP which is used to retrieve the attributes and its value from an XML tag in a SimpleXML object. SimpleXMLElement SimpleXMLElement::attributes ( $namespace, $is_prefix ) Parameter: This function accepts two parameters as mentioned above and described below:

Is it possible to parse simplexmlelement as a string?

It's a time saver for sure, but can fool you into thinking that your SimpleXMLElement is an object. To access the underlying element as a string, it's necessary to make the cast $x = (string)$my_xml_element. Warning to anyone trying to parse XML with a key name that includes a hyphen ie.)

Can I add an XML processing instruction to a simplexmlelement?

It's occasionally useful to add an XML processing instruction to a SimpleXMLElement (treating it as if it were a full document). Adds a new function for SimpleXMLElement class, in order to output HTML code.


1 Answers

You need to pass the namespace uri of the prefix to add child element with prefix :

$item->addChild($key, $value, 'http://base.google.com/ns/1.0');

eval.in demo :

$xml = new SimpleXMLElement('<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0"/>');
$channel = $xml->addChild('channel');
$channel->addChild('title', 'Text');
$channel->addChild('link', 'http://example.com');
$channel->addChild('description', 'An example item from the feed.');

$item = $channel->addChild('item');
$item->addChild('g:foo', 'bar', 'http://base.google.com/ns/1.0');

print $xml->asXML();
like image 178
har07 Avatar answered Oct 27 '22 02:10

har07