I have a php code that use to parse xml data, but it is not working, my php code look as following:
<?php
$xml = '<?xml version="1.0" encoding="utf-8"?>
<videos>
<video>
<id>751985</id>
<embed><![CDATA[somehtmlcode]]></embed>
<thumbs>
<thumb><![CDATA[http://example.com/images/thum01.jpg></thumb>
<thumb><![CDATA[http://example.com/images/thum02.jpg></thumb>
<thumb><![CDATA[http://example.com/images/thum03.jpg></thumb>
<thumb><![CDATA[http://example.com/images/thum04.jpg></thumb>
<thumb><![CDATA[http://example.com/images/thum05.jpg></thumb>
<thumb><![CDATA[http://example.com/images/thum06.jpg></thumb>
<thumb><![CDATA[http://example.com/images/thum07.jpg></thumb>
<thumb><![CDATA[http://example.com/images/thum08.jpg></thumb>
</thumbs>
<link><![CDATA[http://example.com/001]]></link>
<title><![CDATA[Some cool title 1]]></title>
<categories>
<category><![CDATA[Cat1]]></category>
<category><![CDATA[Cat2]]></category>
<category><![CDATA[Cat3]]></category>
<category><![CDATA[Cat4]]></category>
</categories>
<tags>
<tag><![CDATA[tag1]></tag>
<tag><![CDATA[tag2]></tag>
<tag><![CDATA[tag3]></tag>
</tags>
<someone>
</someone>
<duration><![CDATA[8:17]]></duration>
<pubDate><![CDATA[2014-05-14]]></pubDate>
</video>
<video>
<id>751988</id>
<embed><![CDATA[somehtmlcode]]></embed>
<thumbs>
<thumb><![CDATA[http://example.com/images/thum01.jpg></thumb>
<thumb><![CDATA[http://example.com/images/thum02.jpg></thumb>
<thumb><![CDATA[http://example.com/images/thum03.jpg></thumb>
<thumb><![CDATA[http://example.com/images/thum04.jpg></thumb>
<thumb><![CDATA[http://example.com/images/thum05.jpg></thumb>
<thumb><![CDATA[http://example.com/images/thum06.jpg></thumb>
<thumb><![CDATA[http://example.com/images/thum07.jpg></thumb>
<thumb><![CDATA[http://example.com/images/thum08.jpg></thumb>
</thumbs>
<link><![CDATA[http://example.com/001]]></link>
<title><![CDATA[Some cool title 1]]></title>
<categories>
<category><![CDATA[Cat1]]></category>
<category><![CDATA[Cat2]]></category>
<category><![CDATA[Cat3]]></category>
<category><![CDATA[Cat4]]></category>
</categories>
<tags>
<tag><![CDATA[tag1]></tag>
<tag><![CDATA[tag2]></tag>
<tag><![CDATA[tag3]></tag>
</tags>
<someone>
</someone>
<duration><![CDATA[8:17]]></duration>
<pubDate><![CDATA[2014-05-14]]></pubDate>
</video>
</videos>';
$sxe = simplexml_load_string($xml);
foreach ($sxe->video as $videoChild) {
echo $videoChild->id;
echo $videoChild->title;
}
?>
it does not get the value of node, i need to get id, embed, thumb, link, title, without the CDATA tag. any help would be great.
<thumb><![CDATA[http://example.com/images/thum01.jpg></thumb>
the [CDATA] things aren't closed properly, simplexml_load_string() doesn't like that
try
<thumb><![CDATA[http://example.com/images/thum01.jpg]]></thumb>
Related
This is what my XML looks like
<?xml version="1.0" encoding="UTF-8"?>
<item>
<date>May 2013</date>
<html>http://www.link.to/html.html</html>
</item>
I do this in the PHP and it kind of works
$file = '../modules/xml/nl-archive.xml';
$xml = simplexml_load_file($file);
$string = '';
foreach($xml as $item){
$string .= '<a href="'.$item->html .'" ><li>'.$item->date .'</li></a>';
}
Just trying to get the contents of the date tag and html tag to show up for each of the item tags. Been googling and tinkering, can't get it right. What am I doing wrong?
Try changing your XML to have a root with children like:
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>
<date>May 2013</date>
<html>http://www.link.to/html.html</html>
</item>
</items>
Php is just fine as you have it.
Here is the XML that I am working on :
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:noo="http://www.myscheme.com/schema">
<channel>
<item>
<title>A Simple Title</title>
<noo:subcategory>the sub category</noo:subcategory>
<noo:relatedInfos>
<noo:teams>
<noo:team id="3">New York</noo:team>
<noo:team id="4">Las Vegas</noo:team>
</noo:teams>
</noo:relatedInfos>
</item>
</channel>
</rss>
I am doing this php code to get the two "team" but it does not work ($xml has the previous content) :
$xml_datas = simplexml_load_string($xml);
foreach($xml_datas->channel->item as $item){
$noo = $item->children('noo');
echo $noo->team;
}
Do you have any idea why it is not working ?
Thanks
See if this helps:
<?php // RAY_temp_userco.php
error_reporting(E_ALL);
$xml = <<<ENDXML
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:noo="http://www.myscheme.com/schema">
<channel>
<item>
<title>A Simple Title</title>
<noo:subcategory>the sub category</noo:subcategory>
<noo:relatedInfos>
<noo:teams>
<noo:team id="3">New York</noo:team>
<noo:team id="4">Las Vegas</noo:team>
</noo:teams>
</noo:relatedInfos>
</item>
</channel>
</rss>
ENDXML;
$obj = simplexml_load_string($xml);
$ns = $obj->getNamespaces(TRUE);
foreach($obj->channel->item as $item){
$noo = $item->children($ns['noo']);
var_dump($noo);
}
"noo" is just a local alias for that namespace, and the ->children() method (and most XML handling functions) want to know its actual global identifier, which is the URI in the xmlns attribute.
You need to either specify the full identifier of the namespace (i.e. ->children('http://www.myscheme.com/schema')) or set the optional second parameter to tell SimpleXML to look up the prefix (->children('noo', true). The second may be more readable, but it will break if a future document has the same schema, but gives the namespace a different local alias.
Additionally, the team nodes aren't directly under the item node, so you need to traverse further to get them:
// Give the namespace a readable name that won't change
define('NS_NOO', 'http://www.myscheme.com/schema');
$xml_datas = simplexml_load_string($xml);
foreach($xml_datas->channel->item as $item){
$teams = $item->children(NS_NOO)->relatedInfo->teams;
echo $teams->team[0];
}
I have a simple well-formed XML doc that I'm writing to the page using PHP. For some reason the output never includes the title node, and after researching I can't figure this out. If I change the title node to 'heading' or some other name it is included in the output, but when its named 'title', this node is skipped.
Here's the XML doc code...
<?xml version="1.0" encoding="UTF-8"?>
<items>
<product>
<id>cd1</id>
<title>CD One</title>
<description>This is my first CD</description>
<img>/images/sample.jpg</img>
<price>14.99</price>
</product>
</items>
The PHP code looks like this...
<?php
$filename = '../catalog.xml';
$contents = file_get_contents($filename);
echo $contents;
?>
Well, the XML you posted is not valid XML;
The encoding should be in lowercase. Try with this string:
<?xml version="1.0" encoding="utf-8"?>
<items>
<product>
<id>cd1</id>
<title>CD One</title>
<description>This is my first CD</description>
<img>/images/sample.jpg</img>
<price>14.99</price>
</product>
</items>
Validate here: http://validator.w3.org/check
I am very new to Zend Framework. And i have tried to get the XML value but cant make it work.
XML:
<?xml version="1.0" encoding="UTF-8"?>
<result count="2">
<blocks>
<listing>
<title>Title 1</title>
<id>1</id>
</listing>
<listing>
<title>Title 2</title>
<id>2</id>
</listing>
</blocks>
</result>
PHP (to find all the title):
$dom = new Zend_Dom_Query();
$dom->setDocumentXml($result);
$results = $dom->queryXpath('/result/blocks/listing/title');
//$dom->queryXpath('/*/*/listing'); no luck
//$dom->queryXpath('///listing'); no luck
foreach($results as $k)
{
Zend_Debug::dump($k->getAttribute('title')); // empty
echo $k->getDocument(); // shows none
}
Any help?
Using queryXpath('/result/blocks/listing/title') your $k already is the DOMElement that represents the <tile>...</title> elements.
You can retrieve the value via $k->nodeValue. For a DOMElement that's the concatenation of all text nodes in the descendant axis.
foreach($results as $k)
{
Zend_Debug::dump($k->nodeValue); // empty
}
title is a node - not an attribute - attributes are placed inside the tag :)
i'm trying to pass an xml node to a function but can't figure out how - here's my xml markup:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<root>
<form>
<item>
<id>frm1</id>
<dbID>1</dbID>
<visible>1</visible>
</item>
</form>
<form>
<item>
<id>frm2</id>
<dbID>2</dbID>
<visible>1</visible>
</item>
</form>
</root>
when setting up a foreach loop - how's the syntax to iterate through the xml and passing the whole node to a function?
i've tried something like:
foreach($xml as $ctlXML => $value)
{
$ctl = generateCTL($ctlXML);
}
but it doesn't work as it should.
thanks
If you are using SimpleXML it is simple as
$xml = simplexml_load_file($path_to_file);
foreach($xml->form as $form){
$ctl = generateCTL($form->item);
}
Be careful - $form->item is an object