simplexml

SimpleXML access separated text nodes

余生长醉 提交于 2019-12-13 08:14:12
问题 I have an XML file like the following: ... <MyElement>Introduction:<br /> ... </MyElement> ... Are there any way to return "Introduction:" and the rest after </br> of that element? I am using PHP and SimpleXML. Naively use echo $document->MyElement shows "Introduction:...", which mixed up the Introduction line and the content. 回答1: SimpleXML does not have the concept of text-nodes as you might know it from DOMDocument (see as well with this comparison table). So in PHP the option is that you

Using Simplexml's Xpath to filter nodes based upon value in PHP

ぐ巨炮叔叔 提交于 2019-12-13 06:17:09
问题 I'm working on a way to filter certain nodes based upon their value in PHP. I'm trying to return the count of the status nodes that equal 'Funded.' The part I'm not sure about is in using the array (the result of a previous filter ($xml_bio_record[0]) in the new filter ($xml_bio_children)). This code does not return a count for $count_c. How would I filter on status="Funded"? Thanks for any leads. Here is the XML: <?xml version="1.0" encoding="utf-8"?> <data> <record id="1A"> <congrant>

Last XML Items Isn't Removable From The XML

半世苍凉 提交于 2019-12-13 06:12:29
问题 I have two php documents that has this same logic. One document is "uploader.php, which writes to the xml once file is upload. The other document is "modifier.php", which writes to the xml once file the is removed. I have two issue with this logic. First issue is deleting last item in the xml list. It doesn't delete the last item and also duplicates the second to last items. Second issue is with it is logging a error on my "uploader.php". $xml_generator = simplexml_load_file("../file.xml");

How can I get PHP simplexml to save < as itself instead of <

最后都变了- 提交于 2019-12-13 06:11:02
问题 Basically I have a script that updates an xml document in various places... However, I need the text to be in CDATA... So I attempted this: $PrintQuestion->content->multichoice->feedback->hint->Passage->Paragraph->addChild('TextFragment', '<![CDATA[' . $value[0] . ']]>'); Unfortunately when I save the XML back to file, the < and > in cdata come up as their respective < and $gt; codes is there a way to avoid this? Note: Our parser doesn't know how to read the < and > codes, so this is a

How to add keys and values to an array? [closed]

北城以北 提交于 2019-12-13 06:02:15
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . I am using following code to parse the following xml and add the id of each person as key of the array and their names as the values of the array. The code properly works but the array is not. $array = array();

Unset child node makes method disappear

邮差的信 提交于 2019-12-13 05:51:52
问题 I parsed an XML with SimpleXML and wanted to truncate it. This is the method: public function truncate(SimpleXMLElement $xml) { foreach ($xml->children() as $key => $value) { if ( !empty($key) && isset($xml->$key)) { echo "unset $key"; unset($xml->$key); } } echo $xml->asXML(); exit; } After the first unset it throws this: Warning: pat\Persistence\File\Text\XML::truncate(): Node no longer exists in pat/Persistence/File/Text/XML.php on line 36 Well, I didn't want to unset the method. 回答1: Your

Parsing XML with SimpleXML returns nothing

只谈情不闲聊 提交于 2019-12-13 05:36:04
问题 I'm currently trying to parse the MapQuest Traffic API, but when I try to display an incident, nothing appears, and if I do "if empty" in php, it returns empty. Here's the code: <?php $mysongs = simplexml_load_file("http://www.mapquestapi.com/traffic/v1/incidents?key=Fmjtd%7Cluuan1u2nh%2C2a%3Do5-96rw5u&callback=handleIncidentsResponse&boundingBox=$_GET[a], $_GET[b], $_GET[c], $_GET[d]&filters=construction,incidents&inFormat=kvp&outFormat=xml"); echo $mysongs->Incidents[0]->Incident[0]-

simplexml and xpath, read sibling

Deadly 提交于 2019-12-13 04:48:15
问题 I have the following XML file : <?xml version="1.0" encoding="UTF-8" ?> <rss version="2.0"> <channel> <item> [...] <wp:postmeta> <wp:meta_key>_wp_old_slug</wp:meta_key> <wp:meta_value><![CDATA[item-1-slug]]></wp:meta_value> </wp:postmeta> <wp:postmeta> <wp:meta_key>_yoast_wpseo_title</wp:meta_key> <wp:meta_value><![CDATA[item-1-title]]></wp:meta_value> </wp:postmeta> [...] </item> <item> [...] <wp:postmeta> <wp:meta_key>_wp_old_slug</wp:meta_key> <wp:meta_value><![CDATA[item-2-slug]]></wp

How to parse text and image from complex xml

房东的猫 提交于 2019-12-13 04:28:42
问题 I hope you can help me with that. The XML file looks like this: <channel><item> <description> <div> <a href="http://image.com"> <span> <img src="http://image.com" /> </span> </a> Lorem Ipsum is simply dummy text of the printing etc... </div> </description> </item></channel> I can get the contents of the description tag, but when i do that, i get the whole structure which has lots of css in there and i don't want that. What i really need is to parse the href link and the Lorem Ipsum text only.

PHP - Access first index of array of SimpleXML::xpath function

南楼画角 提交于 2019-12-13 04:08:42
问题 I am using SimpleXML to get an element of an XML object specified by tag name and attribute... as follows: $result = $xml->xpath('Stat[@Type="Venue"]'); $venue = $result[0]; This works fine. However... the following shortening gives me an error $venue = $xml->xpath('Stat[@Type="Venue"]')[0]; PHP Parse error: syntax error, unexpected '[' in /var/www/handler.php on line 10 I've got to be being stupid.... but I cannot seem to figure this out. 回答1: You can't use an array like that. You need to