simplexml

Resolve namespaces with SimpleXML regardless of structure or namespace

房东的猫 提交于 2019-12-02 09:33:13
I got a Google Shopping feed like this (extract): <?xml version="1.0" encoding="utf-8" ?> <rss version="2.0" xmlns:g="http://base.google.com/ns/1.0"> ... <g:id><![CDATA[Blah]]></g:id> <title><![CDATA[Blah]]></title> <description><![CDATA[Blah]]></description> <g:product_type><![CDATA[Blah]]></g:product_type> Now, SimpleXML can read the "title" and "description" tags but it can't read the tags with "g:" prefix. There are solutions on stackoverflow for this specific case, using the "children" function. But I don't only want to read Google Shopping XMLs, I need it to be undependend from structure

SimpleXML keeps returning content on CDATA element

╄→гoц情女王★ 提交于 2019-12-02 09:17:29
So another CDATA returning content question. I've seen many answers, but even though I tried them all, I still get only content. In more details: I have an xml file (containing many NewsItem inside): <NewsML> <NewsItem> <NewsComponent> <ContentItem> <DataContent> <body> <body.content> <![CDATA[<p>This is what I am trying to retrieve</p>]]> </body.content> </body> </DataContent> </ContentItem> </NewsComponent> </NewsItem> I am trying to get the content of body.content. Here is my code: $xml = simplexml_load_file('path/to/my/xml.xml',null,LIBXML_NOCDATA); if(count($xml->children()) > 0){ foreach

display data from XML using php simplexml

霸气de小男生 提交于 2019-12-02 09:08:12
问题 I have a piece of XML which is as follows <records count="2"> <record> <firstname>firstname</firstname> <middlename>middlename</middlename> <lastname>lastname</lastname> <namesuffix/> <address> <street-number>demo</street-number> <street-pre-direction/> <street-name>demo</street-name> <street-post-direction/> <street-suffix>demo</street-suffix> <city>demo</city> <state>NY</state> <zip>demo</zip> <zip4>demo</zip4> <county>demo</county> </address> <phonenumberdetails> <phonenumber>demo<

Sorting and grouping SimpleXML Data

怎甘沉沦 提交于 2019-12-02 09:07:54
问题 I am sorting & grouping publication data from an XML file. The methods I am currently using are working fine for the most part , although I feel like there is a more efficient way to do what I am trying to accomplish. Here is a sample of what the target nodes look like: <comic> <id>117</id> <mainsection> <series> <displayname>My Amazing Adventure</displayname> <sortname>My Amazing Adventure</sortname> </series> </mainsection> <issuenr>2</issuenr> <seriefirstletter> <displayname>M</displayname

Can't read XML with simplexml_load_file PHP

天涯浪子 提交于 2019-12-02 09:01:15
So i am trying to parse data from an XML url and insert it into a table using php, which can be seen here , (please keep in mind there are more products than displayed on this page, i am not trying to get it for just this Product,the code below shows how i am parsing all products) but i keep getting the following errors: [EDITED] class DataGrabber { //The URL where data will be extracted from, which is an XML file protected $URL = "http://json.zandparts.com/api/category/GetCategories/44/EUR/"; public function call_api($data) { if(count($data) == 0) return array(); $jsondata = array(); foreach(

simpleXMLElement attributes and foreach

余生长醉 提交于 2019-12-02 08:49:25
If I run in the debugger, the program performs just 2 iterations correctly, and then do an infinite loop. If I exchange the line with foreach, it works correctly. Why? test.xml: <?xml version="1.0" encoding="utf-8"?> <response result="0"> <reports> <get count="2"> <row a="first" b="second" comment="test" c=""/> <row a="first1" b="second2" comment="test2" c=""/> </get> </reports> </response> PHP: $xml = simplexml_load_file('test.xml'); $rows = $xml->xpath('reports/get/row'); foreach($rows as $row){ $item = []; $attr = $row->attributes(); print_r($attr); $i = 0; foreach($attr as $key => $value){

PHP/SimpleXML/XPath get attribute value by another attribute in same element

↘锁芯ラ 提交于 2019-12-02 07:56:06
I have this XML (from a pptx file): <Relationships> <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/image2.png"/> <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/image1.wmf"/> <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout" Target="../slideLayouts/slideLayout1.xml"/> </Relationships> I want to pull the Target attribute from a Relationship element, and I know the Id value. I could do it

“String could not be parsed as XML” php error

北城余情 提交于 2019-12-02 07:46:24
问题 I keep getting this error when I try to create a new instance of SimpleXMLElement . I checked my xml syntax manually and with online tools, and even copy/pasted the example XML file from php.net, but I’m still getting the error. My code: include 'example.php'; $namevalues= new SimpleXMLElement($xmlstr); The example.php: <?php $xmlstr = <<<XML <?xml version='1.0' standalone='yes'?> <movies> <movie> <title>PHP: Behind the Parser</title> <characters> <character> <name>Ms. Coder</name> <actor

Get children attributes using simplexml

倖福魔咒の 提交于 2019-12-02 07:37:00
问题 The xml data looks like this: <feed> <entry> <abc:rank scheme="http://foo.bar">45</abc:rank> <abc:rank scheme="http://foo2.bar">88</abc:rank> </entry> <entry> <abc:rank scheme="http://foo.bar">125</abc:rank> <abc:rank scheme="http://foo2.bar">32</abc:rank> </entry> </feed> I am able to output all of these entries with this code: foreach($xml->entry[$i]->children('abc', true) as $a) { echo $a; } However, if I want to get the one with the content "88" in the first entry, something like foreach(

Weird SimpleXML issue - can't reference nodes by name?

北慕城南 提交于 2019-12-02 07:17:28
I'm trying to parse a remote XML file, which is valid: $xml = simplexml_load_file('http://feeds.feedburner.com/HammersInTheHeart?format=xml'); The root element is feed , and I'm trying to grab it via: $nodes = $xml->xpath('/feed'); //also tried 'feed', without slash Except it doesn't find any nodes. print_r($nodes); //empty array Or any nodes of any kind, so long as I search for them by tag name, in fact: $nodes = $xml->xpath('//entry'); print_r($nodes); //empty array It does find nodes, however, if I use wildcards, e.g. $nodes = $xml->xpath('/*/*[4]'); print_r($nodes); //node found What's