simplexml

PHP SimpleXML large file no extra memory usage

五迷三道 提交于 2019-12-01 22:08:15
问题 In every article about SimpleXML performance and memory usage it is mentioned that all parsed content is stored in memory and that processing large files will lead to large memory usage. But recently I found that processing large files with SimpleXML do not cause large memory usage even more it causes almost none memory usage. There is my test script: <?php error_reporting(E_ALL); ini_set("display_errors", 1); print "OS: " . php_uname() . "\n"; print "PHP version: " . phpversion() . "\n";

PHP array_walk_recursive() for SimpleXML objects?

混江龙づ霸主 提交于 2019-12-01 22:00:55
问题 I would like to apply a function to every node in a SimpleXML object. <api> <stuff>ABC</stuff> <things> <thing>DEF</thing> <thing>GHI</thing> <thing>JKL</thing> </things> </api> //function reverseText($str){}; <api> <stuff>CBA</stuff> <things> <thing>FED</thing> <thing>IHG</thing> <thing>LKJ</thing> </things> </api> How would I apply reverseText() to every node to get the second XML snippet? 回答1: Here the Standard PHP Library can come to the rescue. One option is to use the (little known)

Looping through SimpleXMLElement to access attributes

感情迁移 提交于 2019-12-01 21:41:27
I am trying process data retrieved with SimpleXML and am having great difficulty. I have read numerous threads here about this subject, they all LOOK like what I am doing, but mine are not working. Here's what I've got: <ROOT> <ROWS COMP_ID="165462"> <ROWS COMP_ID="165463"> </ROOT> My code: $xml = simplexml_load_file('10.xml'); foreach( $xml->ROWS as $comp_row ) { $id = $comp_row->COMP_ID; } As I step through this in my debugger, I can see that $id is not set to the string value of COMP_ID, but becomes a SimpleXMLElement itself containing the CLASSNAME object. I've tried many variations of

PHP read decimal integers from xml attributes

浪尽此生 提交于 2019-12-01 21:32:49
Using PHP, I would like to write a function that takes numbers from XML, and multiplies those numbers. However, I don't know how to work with decimal numbers in SimpleXML. PHP $xml = new SimpleXMLElement( '<DOM> <TAB id="ID1" width="30.1" height="0.5" ></TAB> <TAB id="ID2" width="15.7" height="1.8" ></TAB> </DOM>'); foreach ($xml->children() as $second_level) { echo $second_level->attributes()->id."<br>"; echo ($second_level->attributes()->width * 10)."<br>"; echo ($second_level->attributes()->height * 10)."<br>"; } The current (wrong) output: ID1 300 0 ID2 150 10 The correct output should be:

Selecting the children of first result using xpath

怎甘沉沦 提交于 2019-12-01 21:32:39
I have a piece of XML where the same information can appear as a child of different nodes. Such as : <root> <category id=1> <product id="ABC123" > <sizes> <size name="S"/> <size name="M"/> <size name="L"/> <size name="XL"/> <size name="2XL"/> <size name="3XL"/> </sizes> </product> </products> </category> <category id=2> <products> <product id="ABC123" > <sizes> <size name="S"/> <size name="M"/> <size name="L"/> <size name="XL"/> <size name="2XL"/> <size name="3XL"/> </sizes> </product> <product id="PPP543" > <sizes> <size name="S"/> <size name="M"/> <size name="L"/> <size name="XL"/> </sizes>

'getElement(s)By' in the PHP class SimpleXML like in PHP-DomDocument?

Deadly 提交于 2019-12-01 20:06:07
I have made a application using DomDocument & SimpleXML, but the server doesn't support DomDocument (Only SimpleXML). Now I am rewriting it, but there aren't any functions in SimpleXML like "getElementsByTagName" and "getElementById" (I only need those 2). I have searched a lot on php.net & google.com, but can't find one. I am not that good to write my own. So, does anyone know a alternative/function/tip/script for me? :) Thanks in advance. Happily, if SimpleXML doesn't support those DOM-methods, it supports XPath, with the SimpleXMLElement::xpath() method. And searching by tag name or id,

PHP, json_encode, json_decode of SimpleXML Object

吃可爱长大的小学妹 提交于 2019-12-01 19:01:09
A function in my application does the following: Capture Web Page using Snoopy Load result into DOMDocument Load DOMDocument into Simple XML Object Run XPath to isolate section of document required json_encode the result and save to database for later use. My problem arises when recovering this block from the database, and decoding it. I can see the @attributes when I var_dump the object, but cannot find a combination of commands that allows me to access them. Error message is: Fatal error: Cannot use object of type stdClass as array Below is a sample of my object. I have tried, amongst other

Trying to get property of non-object SimpleXML?

自古美人都是妖i 提交于 2019-12-01 18:59:33
I am currently using the following code to retrieve information from a REST api. $url = "http://api.remix.bestbuy.com/v1/products%28upc=".$upc."%29?apiKey=(API KEY)"; $xmlfiledata = file_get_contents("$url"); $xmldata = new SimpleXMLElement($xmlfiledata); $saleprice = $xmldata->products->product->salePrice; echo $saleprice; However, PHP is returning this error. Notice: Trying to get property of non-object in FILE LOCATION on line 132 line 132 is: $saleprice = $xmldata->products->product->salePrice; I have verified that the URL being produced is correct. The xml document in question is here (I

PHP SimpleXML: Remove items with for

喜欢而已 提交于 2019-12-01 18:55:28
问题 I just can remove an item from a simpleXML element with: unset($this->simpleXML->channel->item[0]); but I can't with the a for: $items = $this->simpleXML->xpath('/rss/channel/item'); for($i = count($items); $i > $itemsNumber; $i--) { unset($items[$i - 1]); } some items are removed from $items (Netbeans Debug can confirm that) but when I get the path again (/rss/channel/item) nothing was deleted. What's wrong? 回答1: SimpleXML does not handle node deletion, you need to use DOMNode for this.

PHP array_walk_recursive() for SimpleXML objects?

雨燕双飞 提交于 2019-12-01 18:39:21
I would like to apply a function to every node in a SimpleXML object. <api> <stuff>ABC</stuff> <things> <thing>DEF</thing> <thing>GHI</thing> <thing>JKL</thing> </things> </api> //function reverseText($str){}; <api> <stuff>CBA</stuff> <things> <thing>FED</thing> <thing>IHG</thing> <thing>LKJ</thing> </things> </api> How would I apply reverseText() to every node to get the second XML snippet? Here the Standard PHP Library can come to the rescue. One option is to use the (little known) SimpleXMLIterator . It is one of several RecursiveIterator s available in PHP and a RecursiveIteratorIterator