simplexml

Parsing XML with PHP

廉价感情. 提交于 2019-12-10 18:49:15
问题 I'm trying to parse a jobs feed using PHP's SimpleXML. I've only used JSON before and am having problems getting the parser to work. Here's some sample data: <shrs> <rq url="http://api.simplyhired.com/a/jobs-api/xml_v2/q-comission"> <t>Comission Jobs</t> <dt>2011-02-18T23:58:38Z</dt> <si>0</si> <rpd>10</rpd> <tr>192</tr> <tv>146</tv> <em url=""/> <h> <kw pos="1"/> </h> </rq> <rs> <r> <jt>Virtual Recruiter (IT) - Comission ...</jt> <cn url="">Remedy Intelligent Staffing</cn> <src url="http:/

Why simplexml_load_file does not work relative to host file?

穿精又带淫゛_ 提交于 2019-12-10 17:47:03
问题 I have simplexml_load_file instruction placed withing included PHP file. But this instruction works depending on where I include this file from. Why? Is it possible to make relative file path be interpreted relatively to the file instruction placed in? 回答1: You can always access the current file's full path with the magic __FILE__ constant, so you can write your simplexml_load_file() call like this: <?php simplexml_load_file(dirname(__FILE__).'/file.xml'); Update Since PHP 5.3, there have

simplexml get node value without typecasting

醉酒当歌 提交于 2019-12-10 17:05:09
问题 Is there any way to get a node value from a simplexml object without casting it? $amount = (int)$item->amount; This is not very beautiful in my opinion, I'm searching for a cleaner way but did not find anything so far! //wouldn't this be nice? $amount = $item->amount->getValue(); Thanks in advance. 回答1: Getting the value of a node without having to typecast it? Sure thing! :3 class SimplerXMLElement extends SimpleXMLElement { public function getValue() { return (string) $this; } } Now you

PHP simplexml_load_file - catch file errors

风流意气都作罢 提交于 2019-12-10 15:39:17
问题 Is it possible to catch simplexml file errors? I'm connecting to a webservice that sometimes fails, and I need to make the system skip a file if it returns some http error or something similar. 回答1: Using @ is just plain dirty. If you look at the manual, there is an options parameter: SimpleXMLElement simplexml_load_file ( string $filename [, string $class_name = "SimpleXMLElement" [, int $options = 0 [, string $ns = "" [, bool $is_prefix = false ]]]] ) All option list is available here: http

PHP parsing xml file error

一个人想着一个人 提交于 2019-12-10 15:24:49
问题 I am trying to use simpleXML to get data from http://rates.fxcm.com/RatesXML Using simplexml_load_file() i had errors at times as this website always has weird strings/numbers before and after the xml file. Example: 2000<?xml version="1.0" encoding="UTF-8"?> <Rates> <Rate Symbol="EURUSD"> <Bid>1.27595</Bid> <Ask>1.2762</Ask> <High>1.27748</High> <Low>1.27385</Low> <Direction>-1</Direction> <Last>23:29:11</Last> </Rate> </Rates> 0 I then decided to use file_get_contents and parse it as a

Loading XML using SimpleXML doesn't return attributes on some elements

六眼飞鱼酱① 提交于 2019-12-10 13:22:02
问题 I have loaded an XML file using simplexml_load_file($filePath,'SimpleXMLElement', LIBXML_NOCDATA); And for most of the XML provided it works fine. However, for some of the elements in the XML the attributes are not converted into an '@attributes' array, and are instead missing form the output. Here's a sample: <UI_DEFINITION> <EDIT_PERMISSION>testPermission</EDIT_PERMISSION> <DEFAULT_VALUES> <display>hidden</display> <css_class>generic_css_class</css_class> <title>{tag}</title> <type>string<

Accessing XML properties that have colon in the name with PHP [duplicate]

点点圈 提交于 2019-12-10 11:59:43
问题 This question already has answers here : SimpleXML and namespaces (3 answers) Closed 5 years ago . I'm trying to get video info from a YouTube XML feed such as the title and thumbnail URL. Let's take this feed as an example: http://gdata.youtube.com/feeds/api/videos/CevxZvSJLk8 Here's the code I have now: $videoURL = 'http://gdata.youtube.com/feeds/api/videos/CevxZvSJLk8'; $sxml = simplexml_load_file($videoURL); print_r($sxml); However, that doesn't seem to be including the media:group part

output or write to a xml file?

淺唱寂寞╮ 提交于 2019-12-10 11:44:22
问题 $fp = fopen('data.txt', 'r'); $xml = new SimpleXMLElement('<allproperty></allproperty>'); while ($line = fgetcsv($fp)) { if (count($line) < 4) continue; // skip lines that aren't full $node = $xml->addChild('aproperty'); $node->addChild('postcode', $line[0]); $node->addChild('price', $line[1]); $node->addChild('imagefilename', $line[2]); $node->addChild('visits', $line[3]); } echo $xml->saveXML(); im using this script to convert text file into a xml file, but i want to output it to a file,

iterating SimpleXml xpath result

两盒软妹~` 提交于 2019-12-10 11:25:16
问题 I'm using SimpleXMLElement to read xml returned from a remote server. The results are then parsed with xpath like this: $result = <<<XML <DataImport2Result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.xxx.xxx/Services/DataImport2"> <Number /> <Blocks> <Block> <Code>Fbf</Code> <Fields> <Field> <Code>FinnsIFbf</Code> <Value>1</Value> </Field> </Fields> </Block> </Blocks> </DataImport2Result> XML; $xml = new SimpleXMLElement

trying to read live rss, but its returning same xml

邮差的信 提交于 2019-12-10 10:54:07
问题 I am trying to read xml file from "http://synd.cricbuzz.com/score-gadget/gadget-scores-feed.xml" I understand its caching issue so i have added no-cache but stil its returning same file :( <?php header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Pragma: no-cache"); Header('Pragma: no-cache'); $url = "http://synd.cricbuzz.com/score-gadget/gadget-scores-feed.xml";