simplexml

simplexml error handling php

送分小仙女□ 提交于 2019-12-17 05:07:49
问题 I am using the following code: function GetTwitterAvatar($username){ $xml = simplexml_load_file("http://twitter.com/users/".$username.".xml"); $imgurl = $xml->profile_image_url; return $imgurl; } function GetTwitterAPILimit($username, $password){ $xml = simplexml_load_file("http://$username:$password@twitter.com/account/rate_limit_status.xml"); $left = $xml->{"remaining-hits"}; $total = $xml->{"hourly-limit"}; return $left."/".$total; } and getting these errors when the stream cannot connect:

Forcing a SimpleXML Object to a string, regardless of context

老子叫甜甜 提交于 2019-12-16 22:13:04
问题 Let's say I have some XML like this <channel> <item> <title>This is title 1</title> </item> </channel> The code below does what I want in that it outputs the title as a string $xml = simplexml_load_string($xmlstring); echo $xml->channel->item->title; Here's my problem. The code below doesn't treat the title as a string in that context so I end up with a SimpleXML object in the array instead of a string. $foo = array( $xml->channel->item->title ); I've been working around it like this $foo =

SimpleXML Reading node with a hyphenated name

无人久伴 提交于 2019-12-16 20:05:26
问题 I have the following XML: <?xml version="1.0" encoding="UTF-8"?> <gnm:Workbook xmlns:gnm="http://www.gnumeric.org/v10.dtd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.gnumeric.org/v9.xsd"> <office:document-meta xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:ooo="http://openoffice.org

PHP: XML to JSON fails

自作多情 提交于 2019-12-14 03:06:26
问题 When I try to convert XML(simpleXML) to JSON with json_encode, It works for XML without namesapce. For Example: <ebpacket> <head> <packettype> UserAuthorization</packettype> <staffcode> UserName </staffcode> <pwd> Password </pwd> <env> Evnironment </env> </head> </ebpacket> When I convert XML like below with attributes, json_encode returns an empty json: <soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/> <soapenv:Header /> <soapenv:Body> <ser:processTrans> <xmlValue>

PHP Simple XML remove all Children

青春壹個敷衍的年華 提交于 2019-12-13 17:47:00
问题 i want to remove all children from my xml file before i fill it up again (or create an update but that seemed alot harder). So what i did is $file = "data.xml"; $xml=simplexml_load_file($file); $teller=0; foreach( $entries as $entry ) { foreach ($xml->xpath('//concerts') as $desc) { if($teller == 0) { $lol=$desc->children(); unset($lol); } $concert = $desc->addChild( 'concert' ); $concert->addChild( 'artist', array_shift( $entry ) ); $concert->addChild( 'location', array_shift( $entry ) );

foreach loop over SimpleXMLElement not working, only 1 element as result

戏子无情 提交于 2019-12-13 17:38:38
问题 I have xml file with this structure: $xmlString = ' <plist> <categories> <category> <id>1</id> <parent>0</parent> <description><![CDATA[Test 1]]></description> </category> <category> <id>2</id> <parent>1</parent> <description><![CDATA[Test 1.1]]></description> </category> <category> <id>3</id> <parent>1</parent> <description><![CDATA[Test 1.2]]></description> </category> </categories> </plist>'; Now I'm trying to build an array this way: $xmlData = new SimpleXMLElement($xmlString); $results =

Simplexml: parsing HTML leaves out nested elements inside an element with a text node

谁都会走 提交于 2019-12-13 16:32:54
问题 I'm trying to parse a specific html document, some sort of a dictionary, with about 10000 words and description. It went well until I've noticed that entries in specific format doesn't get parsed well. Here is an example: <?php $html = ' <p> <b> <span>zot; zotz </span> </b> <span>Nista; nula. Isto <b>zilch; zip.</b> </span> </p> '; $xml = simplexml_load_string($html); var_dump($xml); ?> Result of var_dump() is: object(SimpleXMLElement)#1 (2) { ["b"]=> object(SimpleXMLElement)#2 (1) { ["span"]

PHP SVG editing

匆匆过客 提交于 2019-12-13 16:23:38
问题 For example I am trying to read the style block from an svg, I can get the type but not the string. $svgTemplate = new SimpleXMLElement($_POST['SvgTemplateImport']); $svgTemplate->registerXPathNamespace('svg', 'http://www.w3.org/2000/svg'); $svgTemplate->registerXPathNamespace('xlink', 'http://www.w3.org/1999/xlink'); //I tried a bunch of things and this how far I got $test[] = $svgTemplate->xpath('/svg:svg/svg:style'); $test[] = $result[0][0]['type']; var_dump($test); How to retrieve the

data lost in simplexml_load_string

[亡魂溺海] 提交于 2019-12-13 15:27:40
问题 I am reading xml with this code. $xml = simplexml_load_string($data); it reads correctly, and can access to data via $xml->title etc. but i need to access to CDATA and <media:xxx> tags. example of xml: pastie from: http://www.metacafe.com/api/item/4779040/ is possible to parse that data? how? 回答1: $xml = simplexml_load_string($data); $namespacesMeta = $xml->getNamespaces(true); $mediaXML = $xml->children($namespacesMeta['media']); 回答2: You need to look into SimpleXml and Namespaces Try this -

Parse soap response using PHP

℡╲_俬逩灬. 提交于 2019-12-13 09:03:19
问题 I am trying to get all the elements and values as array from the below soap response. But I am only getting elements but no values. I have used this parser to get the elements and values. Thanks in advance. Here is the code I am using, $response = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header/> <soapenv:Body> <toy:SearchReq xmlns:toy="www.something.com/toy" xmlns:gen="www.something.com/gen"> <gen: manufacturedIn="SomePlace"/> <toy:SearchToy>