simplexml

Turn OFF self-closing tags in SimpleXML for PHP?

放肆的年华 提交于 2019-12-23 21:26:31
问题 I'm building an XML document with PHP's SimpleXML extension, and I'm adding a token to the file: $doc->addChild('myToken'); This generates (what I know as) a self-closing or single tag: <myToken/> However, the aging web-service I'm communicating with is tripping all over self-closing tags, so I need to have a separate opening and closing tag: <myToken></myToken> The question is, how do I do this, outside of running the generated XML through a preg_replace ? 回答1: From the documentation at

SimpleXMLElement get first XPath Element in one line [duplicate]

一曲冷凌霜 提交于 2019-12-23 20:21:55
问题 This question already has an answer here : Selecting only the first item of an xpath result set in PHP (1 answer) Closed 6 years ago . Just trying to figure a shorter way to do this: I'm using simpleXMLElement to parse an xml file and it's aggravating to have to call two lines to process an array when I know what node I want. Current code: $xml = new SimpleXMLElement($data); $r = $xml->xpath('///givexNumber'); $result["cardNum"] = $r[0]; What I would like to do would be something like I can

Why am I getting an array of SimpleXMLElement Objects here?

泄露秘密 提交于 2019-12-23 20:02:50
问题 I have some code that pulls HTML from an external source: $doc = new DOMDocument(); @$doc->loadHTML($html); $xml = @simplexml_import_dom($doc); // just to make xpath more simple $images = $xml->xpath('//img'); $sources = array(); Then, if I add all of the sources with this code: foreach ($images as $i) { array_push($sources, $i['src']); } echo "<pre>"; print_r($sources); die(); I get this result: Array ( [0] => SimpleXMLElement Object ( [0] => /images/someimage.gif ) [1] => SimpleXMLElement

Parse XML with a colon attribute in PHP

半世苍凉 提交于 2019-12-23 19:53:34
问题 I'd have the following XML structure: <entry> <id im:id="595831580">blabla</id> </entry> Now I want to parse the id 595831580. I tried: $idAtt = $xml->entry->id; $id = $idAtt->attributes(); $id2 = $id['im:id']; But this does not work :( How can I fix it? 回答1: Alright. You can't use namespace in offsetGet method of SimpleXMLElement, but you can in attributes method: echo $xml->entry->id->attributes("im",TRUE)->id; Check out this comment for one more demo. 回答2: Not sure but maybe something like

Cannot parse CDATA with SimpleXML

女生的网名这么多〃 提交于 2019-12-23 19:26:11
问题 I have the following XML code: <para> <![CDATA[ <?php $data = '<?xml version="1.0"?> <root>content</root>'; $sxe = simplexml_load_string($data); var_dump($sxe); ?> ]]> </para> I want to parse the CDATA section to take this result: Content: <?php $data = '<?xml version="1.0"?> <root>content</root>'; $sxe = simplexml_load_string($data); var_dump($sxe); ?> I use the following but it doesn't work. $xml='sxml.xml'; $book = simplexml_load_file($xml, 'SimpleXMLElement', LIBXML_NOCDATA); $para =

Large PHP for loop with SimpleXMLElement very slow: memory issues?

落爺英雄遲暮 提交于 2019-12-23 19:12:03
问题 I currently have a bit of PHP code that basically pulls in data from an xml file and creates simple xml object using $products = new SimpleXMLElement($xmlString); I then loop over this code with a for loop within which I set the product details for each product in the XML document. Then it is saved to a mySql database. Whilst running this script the products added reduce in frequency until they eventually stop before reaching the maximum. I have tried running garbage collection in intervals,

Getting attributes from XML in PHP

≯℡__Kan透↙ 提交于 2019-12-23 18:50:40
问题 I'm trying to get attributes from an XML file using this code: $xmlFile = "http://weather.aero/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=".$_GET['station']."&hoursBeforeNow=1"; $xml = simplexml_load_file($xmlFile); This is the XML file: <response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XML-Schema-instance" version="1.2" xsi:noNamespaceSchemaLocation="http://weather.aero/schema/metar1_2.xsd"> <request

Change text of root SimpleXML Element

廉价感情. 提交于 2019-12-23 18:07:57
问题 I'm trying to create a simple wrapper function for outputting my errors in XML for an existing Flash application. I've already read that SimpleXMLElement is not necessarily intended for creating a new XML document but it's working fine for me so far and I am basically replacing concatenated strings. Up until now I've had no problems iterating and adding/modifying attribues, values, etc. In this example I would like to see my output look like this: <ERROR>There is an error</ERROR> But I am

PHP SimpleXML->addChild - unwanted empty namespace attribute

谁都会走 提交于 2019-12-23 17:56:32
问题 I'm attempting to use SimpleXML's addChild method of the SimpleXMLElement (actually a SimpleXMLIterator which is a subclass of SimpleXMLElement) to add child elements. My problem is that the source document contains a mixture of elements with namespaces and without. Here's a simple (no pun intended) example: <?xml version="1.0" encoding="UTF-8"?> <ns1:a xmlns:ns1="http://www.abc.com"> </ns1:a> The PHP code is: $it = new SimpleXMLIterator ('./test.xml', 0, true); $it->addChild('d', 'another!')

How to search in XML file using php?

折月煮酒 提交于 2019-12-23 17:56:03
问题 I don't know if there is a method to search in xml file. For instance. I want to get the Value using the Name from AttrList and the ProductCode . Is it possible ? This is how my xml look likes: <Product> <ProductCode>70-14UF44-00</ProductCode> <Vendor>NBM</Vendor> <ProductType>Soft. Unsorted application</ProductType> <ProductCategory>Software</ProductCategory> <ProductDescription>{Bluetooth Driver IVT V.1.4.9.3, 1pk, Full Package, OEM, 1pk for 12M3W/15G3WS, 1pk, 1pk}</ProductDescription>