domdocument

Reading XML data in PHP, simple

感情迁移 提交于 2020-01-15 06:15:05
问题 <?xml version="1.0" encoding="utf-8"?> <mainXML> <items> <item category="Dekorationer" name="Flot væg" description="Meget flot væg. Passer alle stuer." price="149" /> <item category="Fritid" name="Fodbold" description="Meget rund bold. Rørt af messi." price="600" /> </items> </mainXML> How can i read this? So i can make like a php loop that outputs the category, name and description for example? I tried and started out with this: $doc = new DOMDocument(); $doc->load( 'ex.xml' ); $items = $doc

DOM replaceChild not replacing all specified elements

删除回忆录丶 提交于 2020-01-14 09:06:24
问题 Consider the following code: $xml = <<<XML <root> <region id='thisRegion'></region> <region id='thatRegion'></region> </root> XML; $partials['thisRegion'] = "<p>Here's this region</p>"; $partials['thatRegion'] = "<p>Here's that region</p>"; $DOM = new DOMDocument; $DOM->loadXML($xml); $regions = $DOM->getElementsByTagname('region'); foreach( $regions as $region ) { $id = $region->getAttribute('id'); $partial = $DOM->createDocumentFragment(); $partial->appendXML( $partials[$id] ); $region-

xml insertion at specific point of xml file

て烟熏妆下的殇ゞ 提交于 2020-01-13 04:41:05
问题 I want to insert the following line into my xml file: <?xml-stylesheet type="text/xsl" href="http://example.com/livesearch.xsl"?> immediately after: <?xml version="1.0" encoding="UTF-8" ?> in my xml file. Currently I use this (awful) method: $G['xml'] = str_replace('<?xml version="1.0" encoding="UTF-8" ?>', '<?xml version="1.0" encoding="UTF-8" ?><?xml-stylesheet type="text/xsl" href="http://example.com/livesearch.xsl"?>', $G['xml']); What is the correct way to do this with DomDocument in php

PHP DomDocument editing all links

邮差的信 提交于 2020-01-13 03:25:10
问题 I am using the following code to grab html from another page and place it into my php page: $doc = new DomDocument; // We need to validate our document before refering to the id $doc->validateOnParse = true; $doc->loadHtml(file_get_contents('{URL IS HERE}')); $content = $doc->getElementById('form2'); echo $doc->SaveHTML($content); I want to change all instances of <a href="/somepath/file.htm"> so that I can prepend to it the actual domain instead. How can I do this? So, it would need to

How to change root of a node with DomDocument methods?

眉间皱痕 提交于 2020-01-11 07:31:10
问题 How to only change root's tag name of a DOM node? In the DOM-Document model we can not change the property documentElement of a DOMElement object, so, we need "rebuild" the node... But how to "rebuild" with childNodes property? NOTE: I can do this by converting to string with saveXML and cuting root by regular expressions... But it is a workaround, not a DOM-solution. Tried but not works, PHP examples PHP example (not works, but WHY?): Try-1 // DOMElement::documentElement can not be changed,

How can I easily combine two XML documents with the same parent node into one document?

爷,独闯天下 提交于 2020-01-10 05:18:05
问题 I've decided there's no way to do this with SimpleXMLElements. I have been reading the PHP DOMDocument manual, and I think I could do it with iteration, but that seems inefficient. Is there a better way that's not occurring to me? Psuedocode-ish iterative solution: // two DOMDocuments with same root element $parent = new ... $otherParent = new ... $children = $parent->getElementByTagName('child'); foreach ($children as $child) { $otherParent->appendChild($child); } For clarity, I have two XML

Help parsing XML with DOMDocument

≡放荡痞女 提交于 2020-01-10 04:00:15
问题 I am trying to parse a youtube playlist field. The URL is: http://gdata.youtube.com/feeds/api/playlists/664AA68C6E6BA19B?v=2 I need: Title, Video ID, and Default thumbnail. I can easily get the title but I'm a little lost when it comes to the nested elements $data = new DOMDocument(); if($data->load("http://gdata.youtube.com/feeds/api/playlists/664AA68C6E6BA19B?v=2")) { foreach ($data->getElementsByTagName('entry') as $video) { $title = $video->getElementsByTagName('title')->item(0)-

Save XML file without Declaration - with specific address

微笑、不失礼 提交于 2020-01-07 04:00:43
问题 I am trying to find a way to save the XML file once edited, without including the declaration. But i need to be able to set an address. As i am saving over the original XML, and saving over a temp location one (duplicate of the original for javascript to access, as the original is in a local file). So i tried the $dom->saveXML($xml->documentElement); but comes out with some save errors. This is the php page that gets the form data and saves it to the currently loaded xml, then saves it and

How to create and append multiple dom elements efficiently

Deadly 提交于 2020-01-07 02:52:07
问题 I'm building 10 buttons on the DOM and want to see if there is a more efficient way of building them because it seems strange to be calling createElement and appendChild 10 times. <script> function makeAlertButtons () { var container = document.createElement("div") container.setAttribute('id', "container") document.getElementsByTagName('body')[0].appendChild(container) for (var x = 1; x <=10; x++) { var butt = document.createElement("button") butt.appendChild(document.createTextNode(x))

How can i read the first XML comment in PHP?

本秂侑毒 提交于 2020-01-07 02:16:49
问题 How can I read the first and only comment in PHP: <?xml version="1.0" encoding="UTF-8"?> <!-- read this --> <root> </root> using DOMDOcument object? $xml = new DOMDocument(); $libxml_error = 'Il file %s non risulta ben formato (%s).'; // Per evitare i warning nel caso di xml non ben formato occorre sopprimere // gli errori if (!@$xml -> load($xml_file_path)) $error = sprintf($libxml_error, $xml_file_path, libxml_get_last_error()->message); // Read the fist comment in xml file 回答1: What about