simplexml

PHP simpleXML parsing

时间秒杀一切 提交于 2019-12-12 19:23:09
问题 I need currency conversion, euro to dollar. The European Central bank provides the rates here: http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml I can get the USD rate by using the first node, but what if they change the order? Do I need something more reliable? I have no idea how.. $xml = @simplexml_load_file('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'); echo "dollar: " . $xml->Cube->Cube->Cube[0]->attributes()->rate; 回答1: Just use XPath to get any node with the

SimpleXML - echo / print_r return different values

梦想的初衷 提交于 2019-12-12 17:10:54
问题 I am trying to convert some xml into a json object using PHP. This should be working, yet for some bizarre reason it is failing. Could someone provide some input. // Loop Through images and return the right one. $i = 1; foreach($page->image as $image) { if ($i == $_GET['id']) { echo json_encode(array( 'background' => $image['bgColor'], 'image' => $image['source'], 'caption' => $image['caption'] )); } $i++; } This code returns the following. {"background":{"0":"000033"}, "image":"0":

Loading file with simplexml_load_file

懵懂的女人 提交于 2019-12-12 13:05:20
问题 I am puzzled why this is not working yet i can echo the test.xml <?php $xml = simplexml_load_file('test.xml'); $movies = new SimpleXMLElement($xml); echo $movies->movie[1]->plot; ?> 回答1: There is no need to do both, simplexml_load_file and create a new SimpleXML object. simplexml_load_file already interprets an XML file into an object. (Keep in mind, it does not accept an XML string) $movies = simplexml_load_file('test.xml'); Alternatively, you may directly load an XML string into a SimpleXML

Remove starting and ending spaces from XML elements

好久不见. 提交于 2019-12-12 11:00:56
问题 How can I remove all spacing characters before and after a XML field? <data version="2.0"> <field> 1 </field> <field something=" some attribute here... "> 2 </field> </data> Notice that spacing before 1 and 2 and 'some attribute here...', I want to remove that with PHP. if(($xml = simplexml_load_file($file)) === false) die(); print_r($xml); Also the data doesn't appear to be string, I need to append (string) before each variable. Why? 回答1: You may want to use something like this: $str = file

PHP - Duplicate XML node using Simple XML

扶醉桌前 提交于 2019-12-12 07:24:42
问题 I need to load an XML source using Simple XML, duplicate an existing node with all his children, then customize an attribute of this new node before rendering XML. Any suggestion? 回答1: SimpleXML can't do this, so you'll have to use DOM. The good news is that DOM and SimpleXML are two sides of the same coin, libxml. So no matter whether you're using SimpleXML or DOM, you're working on the same tree. Here's an example: $thing = simplexml_load_string( '<thing> <node n="1"><child/></node> </thing

Retrieving children from XML with PHP

风流意气都作罢 提交于 2019-12-12 05:46:25
问题 Helle there, There is a post: https://stackoverflow.com/questions/5816786/counting-nodes-in-a-xml-file-using-php I have the same question, but instead of count, I want to echo it. I have this code in xml: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Row> <ModeNumber>1</ModeNumber> <Mode>online</Mode> </Row> <Row> <ModeNumber>2</ModeNumber> <Mode>mmorpg</Mode> </Row> And this as PHP: $xml = simplexml_load_file("include

How is it possible to modify the whole body part of an xml document loaded in php via simplexml?

末鹿安然 提交于 2019-12-12 05:27:18
问题 Assume the following xml document <technicaldata template="123"> <name> <![CDATA[Blub1]]> </name> <numbers> <![CDATA[1 2 3 4 5]]> </numbers> <shortinfo> <![CDATA[ha ha ha ha ha ha ha ha.]]> </shortinfo> <detailedinfo> <![CDATA[hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi]]> </detailedinfo> </technicaldata> Now I load the file via simplexml; $data='test.xml';

How to parse value `@attribute` from a SimpleXMLObject in PHP

↘锁芯ラ 提交于 2019-12-12 05:12:35
问题 I am trying to parse the starkoverflow.com/feeds/tag/{$tagName} . This is my code: <?php $xml = file_get_contents("http://stackoverflow.com/feeds/tag/php"); $simpleXml = simplexml_load_string($xml); $attr = $simpleXml->entry->category->@attributes; ?> When I execute the above code it gives me a error, Parse error: syntax error, unexpected '@', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in D:\wamp\www\success\protoT.php on line 4 So, My question is how to get the

XML simplexml_load_file foreach not looping

∥☆過路亽.° 提交于 2019-12-12 04:57:40
问题 I am stuck here with this problem. I have an XML file that LOOKS LIKE THIS: <?xml version="1.0" encoding="UTF-8"?> <ListVehicleInventory xmlns="http://www.starstandards.org/STAR" xmlns:oa="http://www.openapplications.org/oagis" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.starstandards.org/STAR/STAR/Rev1.1/BODs/ListVehicleInventory.xsd" revision="1.0" release="8.1" environment="Production" lang="en-US"> <ApplicationArea> <Sender> <Component>XPO<

How parsing out XML file with namespaces?

梦想的初衷 提交于 2019-12-12 04:38:34
问题 I know similar questions were posted before, but I can't parse out this XML file with namespaces. Here is the link to it because it's too big to post here: https://tsdrapi.uspto.gov/ts/cd/casestatus/sn86553893/info.xml I tried using simplexml_load_file but that does not create xml object. Then I found similar problems and try something like this, provided I already downloaded file named it 86553893.xml Here is my php code: $xml= new SimpleXMLElement("86553893.xml"); foreach($xml->xpath('//com