simplexml

PHP - Processing Invalid XML

可紊 提交于 2019-12-17 09:52:16
问题 I'm using SimpleXML to load in some xml files (which I didn't write/provide and can't really change the format of). Occasionally (eg one or two files out of every 50 or so) they don't escape any special characters (mostly &, but sometimes other random invalid things too). This creates and issue because SimpleXML with php just fails, and I don't really know of any good way to handle parsing invalid XML. My first idea was to preprocess the XML as a string and put ALL fields in as CDATA so it

Getting cdata content while parsing xml file

 ̄綄美尐妖づ 提交于 2019-12-17 09:52:10
问题 I have an xml file <?xml version="1.0" encoding="utf-8"?> <xml> <events date="01-10-2009" color="0x99CC00" selected="true"> <event> <title>You can use HTML and CSS</title> <description><![CDATA[This is the description ]]></description> </event> </events> </xml> I used xpath and and xquery for parsing the xml. $xml_str = file_get_contents('xmlfile'); $xml = simplexml_load_string($xml_str); if(!empty($xml)) { $nodes = $xml->xpath('//xml/events'); } i am getting the title properly, but iam not

PHP SimpleXML + Get Attribute

独自空忆成欢 提交于 2019-12-17 09:49:53
问题 The XML I am reading looks like this: <show id="8511"> <name>The Big Bang Theory</name> <link>http://www.tvrage.com/The_Big_Bang_Theory</link> <started>2007-09-24</started> <country>USA</country> <latestepisode> <number>05x23</number> <title>The Launch Acceleration</title> </latestepisode> </show> To get (for example) The number of the latest episode, I would do: $ep = $xml->latestepisode[0]->number; This works just fine. But what would I do to get the ID from <show id="8511"> ? I have tried

Basic simpleXML working example?

人盡茶涼 提交于 2019-12-17 09:37:12
问题 It looks like there are many problems with simpleXML in PHP. I'm running the latest version of php on Windows and I just can not get the basic examples of simpleXML to work as in the documentation. My xml file is: <?xml version="1.0" encoding="ISO-8859-1"?> <programme> <title>Billy Bushwaka</title> <episodeNumber>2</episodeNumber> <description>Billy Bushwaka entertains</description> <url>play.swf</url> </programme> My PHP program is: <?php $xml = simplexml_load_file("local.xml"); $result =

PHP convert XML to JSON group when there is one child

本秂侑毒 提交于 2019-12-17 07:56:26
问题 I use this PHP class to convert XML to JSON:http://www.ibm.com/developerworks/library/x-xml2jsonphp/ For example for this XML: <?xml version="1.0" encoding="UTF-8"?> <searchResult> <status>OK</status> <users> <user> <userName>johndoe</userName> </user> <user> <userName>johndoe1</userName> <fullName>John Doe</fullName> </user> <user> <userName>johndoe2</userName> </user> <user> <userName>johndoe3</userName> <fullName>John Doe Mother</fullName> </user> <user> <userName>johndoe4</userName> <

PHP convert XML to JSON group when there is one child

百般思念 提交于 2019-12-17 07:55:46
问题 I use this PHP class to convert XML to JSON:http://www.ibm.com/developerworks/library/x-xml2jsonphp/ For example for this XML: <?xml version="1.0" encoding="UTF-8"?> <searchResult> <status>OK</status> <users> <user> <userName>johndoe</userName> </user> <user> <userName>johndoe1</userName> <fullName>John Doe</fullName> </user> <user> <userName>johndoe2</userName> </user> <user> <userName>johndoe3</userName> <fullName>John Doe Mother</fullName> </user> <user> <userName>johndoe4</userName> <

How to Paginate lines in a foreach loop with PHP

人走茶凉 提交于 2019-12-17 07:38:21
问题 Using the following code to display a list of friends from my twitter profile. Id like to only load a certain number at a time, say 20, then provide pagination links at the bottom for First 1-2-3-4-5(however many divided by limit) Last $xml = simplexml_load_string($rawxml); foreach ($xml->id as $key => $value) { $profile = simplexml_load_file("https://twitter.com/users/$value"); $friendscreenname = $profile->{"screen_name"}; $profile_image_url = $profile->{"profile_image_url"}; echo "<a href=

Unable to add namespace to an attribute with PHP's SimpleXML

别说谁变了你拦得住时间么 提交于 2019-12-17 07:38:05
问题 I am creating an Atom feed, when I tried below to add xmlns:i as an attribute - $node->addAttribute("xmlns:i","http://www.w3.org/2001/XMLSchema-instance"); I got this as an output - i="http://www.w3.org/2001/XMLSchema-instance" "xmlns:" part was cut off. do I need to escape the : -character? Or is they any other way to add this namespace? 回答1: If you want to add an attribute from the namespace/prefix i to $node don't bother declaring the namespace beforehand. Just use the third parameter of

SimpleXML: Working with XML containing namespaces

感情迁移 提交于 2019-12-17 06:54:32
问题 I am trying to get to the geo information off the google-picasa API. This is the original XML: <georss:where> <gml:Point> <gml:pos>35.669998 139.770004</gml:pos> </gml:Point> </georss:where> I already have come this far, with: $ns_geo=$item->children($namespace['georss']); $geo=$ns_geo->children($namespace['gml']); var_dump($geo) will output object(SimpleXMLElement)#34 (1) { ["Point"]=> object(SimpleXMLElement)#30 (1) { ["pos"]=> string(18) "52.373801 4.890935" } } but echo (string)$geo-

SimpleXML SOAP response Namespace issues

此生再无相见时 提交于 2019-12-17 06:51:38
问题 After spending SEVERAL frustrated hours on this I am asking for your help. I am trying to get the content of particular nodes from a SOAP response. The response is <?xml version="1.0" encoding="UTF-8"?> <env:Envelope xmlns:env="<a href="http://www.w3.org/2003/05/soap-envelope">http://www.w3.org/2003/05/soap-envelope</a>"<xmlns:ns1="<a href="http://soap.xxxxxx.co.uk/">http://soap.xxxxxx.co.uk/</a>"> <env:Body> <ns1:PlaceOrderResponse> <xxxxxOrderNumber></xxxxxOrderNumber> <ErrorArray> <Error>