Simple Xml - Dealing With Colons in Nodes

Simple XML - Dealing With Colons In Nodes

The solution is explained in this nice article. You need the children() method for accessing XML elements which contain a namespace. This code snippet is quoted from the article:

$feed = simplexml_load_file('http://www.sitepoint.com/recent.rdf'); 
foreach ($feed->item as $item) {
$ns_dc = $item->children('http://purl.org/dc/elements/1.1/');
echo $ns_dc->date;
}

PHP library for parsing XML with a colons in tag names?

Say you have some xml like this.

<xhtml:div>
<xhtml:em>italic</xhtml:em>
<date>2010-02-01 06:00</date>
</xhtml:div>

You can access 'em' like this: $xml->children('xhtml', true)->div->em;

however, if you want the date field, this: $xml->children('xhtml', true)->div->date; wont work, because you are stuck in the xhtml namespace.

you must execute 'children' again to get back to the default namespace:

$xml->children('xhtml', true)->div->children()->date;

xml element name with colon

Okay, here it is.

XmlDocument.CreateElement("prefix", "name", "uri");

reference here if it helps someone else: http://msdn.microsoft.com/en-us/library/c22k3d47.aspx 1

Read colon tags values XML PHP

You should use the children() on the $item element to get it's child-elements:

$str =<<< END
<item>
<title> TITLE </title>
<itunes:author> AUTHOR </itunes:author>
<description> TEST </description>
<itunes:subtitle> TEST </itunes:subtitle>
<itunes:summary> TEST </itunes:summary>
<itunes:image href="yoyoyoyo.jpg"/>
<pubDate> YESTERDAY </pubDate>
<itunes:block>no</itunes:block>
<itunes:explicit>no</itunes:explicit>
<itunes:duration>99:99:99</itunes:duration>
<itunes:keywords>key, words</itunes:keywords>
</item>
END;

$result = @simplexml_load_string($str);

$items = $result->xpath("//item");

foreach ($items as $item) {
echo $item->title . "\n";
echo $item->pubDate . "\n";
echo $item->children()->{'itunes:duration'} . "\n";
}

Output:

TITLE
YESTERDAY
99:99:99

Get certain values from XML feed with a colon in the node name

The problem comes in when using namespaces, it's quite simple to deal with them in any XML system, here I've used SimpleXML. I've also assuming it's Channel that is repeated.

To use namespaces, you need to register them with the XML system so that it knows how to associate them with the search, so here I use newznab as the prefix to attr. But this is what you see in the XML document, so it makes it easier to read. The XPath uses [@name='size'] to make it find the instance of attr which has this attribute/value combination - and then it returns the value attribute.

$xml = simplexml_load_file('NewFile.xml');
$xml->registerXPathNamespace("atom", "http://www.w3.org/2005/Atom");
$xml->registerXPathNamespace("newznab", "http://www.newznab.com/DTD/2010/feeds/attributes/");

foreach( $xml->channel as $channel ){
echo "Channel title=".(string)$channel->title.PHP_EOL;
echo "size=".(string)$channel->xpath("descendant::newznab:attr[@name='size']/@value")[0].PHP_EOL;
echo "usenetdate=".(string)$channel->xpath("descendant::newznab:attr[@name='usenetdate']/@value")[0].PHP_EOL;
}

XML node values where xml data has colon syntax

That "colon syntax" is an XML namespace that is part of your XML document, therefore you need to respect and use it when querying, too.

I'm not 100% sure what you really want to get from that XML - my sample code here iterates over the <storename:column> nodes and extracts the Name and Value attributes from them:

; WITH XMLNAMESPACES('http://www.storename.com/soap/rpc' AS ns)
SELECT
ColName = rec.value('@name','varchar(50)'),
ColValue = rec.value('@value','varchar(50)')
FROM
@xmldata.nodes('/ns:results/ns:resultset/ns:row/ns:column') AS x(rec)

XPath query for XML node with colon in node name

You need to learn about namespaces and how to define/register a namespace in your XPath engine so that you can then use the associated prefix for names in that registered namespace. There are plenty of questions in the xpath tag asking how to use names that are in a namespace -- with good answers. Search for them.

A very rough answer (ignoring namespaces at all) is:

//*[name()='media:thumbnail']

SimpleXML parse get past colon PHP

The XML as you posted it is syntactically incorrect. There's no closing </entry> tag and there are no namespace definitions in it. This caused many a warning when attempting to come up with an answer.

Once I fixed those issues in the XML I used the following to get what you seem to be looking for:

foreach($pictureBoxXMLFeed->xpath('//content') as $content){
$hey = ($content->children('media', true)->content->children('ilink', true)->parent_id);
echo $hey;
echo '<br/>';
}

How to get XML nodes content when names include special Characters?

You can access the tags with colons in them (aka namespaces) using the children() method:

echo $xml->Day->SpecialDeals->children('a', true)->string[0];

Demo!

This SitePoint article explains namespaces in detail.



Related Topics



Leave a reply



Submit