domdocument

How to sort xml child via attribute value PhP

喜夏-厌秋 提交于 2021-02-05 08:18:57
问题 I have an XML file through PHP that I am trying to list a specific child "word" in ascending order in a div, according to the attribute "word". I put sort($word); without sucess Here is my code: <?php if(isset($_POST['submitSave'])) { // Disable errors due to empty xml files error_reporting(E_ALL & ~E_WARNING); $domDoc = new DOMDocument('1.0'); $domDoc->preserveWhiteSpace = false; $domDoc->formatOutput = true; $xpath = new DOMXpath($domDoc); // load xml file try { $domDoc->load('./data

PHP DOMDocument replace content by div id

前提是你 提交于 2021-02-04 19:56:29
问题 I am trying to repalce content of div like in jQuery .html(). This is what I have done so far: $html = " <html> <body> <div id="main"> <div id="text"> <p>This is some text</p> </div> </div> </body> </html> "; $doc = new DOMDocument(); $doc->loadHTML($html); // change div id "text" contents // $("#text").html("<p>This is some new text</p>"); echo $doc->saveHTML(); Any help would be appreciated! Thank you. 回答1: You can use the following code, you just need to change the nodeValue $doc->loadHTML

PHP DOMDocument replace content by div id

别等时光非礼了梦想. 提交于 2021-02-04 19:55:52
问题 I am trying to repalce content of div like in jQuery .html(). This is what I have done so far: $html = " <html> <body> <div id="main"> <div id="text"> <p>This is some text</p> </div> </div> </body> </html> "; $doc = new DOMDocument(); $doc->loadHTML($html); // change div id "text" contents // $("#text").html("<p>This is some new text</p>"); echo $doc->saveHTML(); Any help would be appreciated! Thank you. 回答1: You can use the following code, you just need to change the nodeValue $doc->loadHTML

XSLTProcessor::transformToUri(): Memory allocation failed : reaching arbitrary MAX_URI_LENGTH limit

萝らか妹 提交于 2021-01-29 17:36:45
问题 I have some XML files that I need to "transform" in Html and display on screen. I have developed a simple script that works -almost- all of the times, using DOMDocument and XSLTProcessor . The problem is that sometimes it gives this error, and the resulting html is only a part of the complete content: XSLTProcessor::transformToUri(): Memory allocation failed : reaching arbitrary MAX_URI_LENGTH limit in /var/www/test/index.php on line 14 This is a working copy of my script, which gives the

How to loop through XML-nodes and validate if values exists?

三世轮回 提交于 2021-01-29 08:09:14
问题 I have through an API fetched my data as an XML, and I wish to cycle through nodes (there are several of the same type) and add them to certain fields/a table. Example from the XML-file: <HistRating xmlns=""> <EndrAr>2020</EndrAr> <EndrMnd>7</EndrMnd> <Rating>A</Rating> </HistRating> <HistRating xmlns=""> <EndrAr>2019</EndrAr> <EndrMnd>6</EndrMnd> <Rating>A</Rating> </HistRating> I have tried the following format (at this point the XML I need is in a string in xmlDoc xmlDoc = CreateObject(

Why do these two DOMDocument functions behave differently?

99封情书 提交于 2021-01-28 06:18:33
问题 There are two approaches to getting the outer HTML of a DOMDocument node suggested here: How to return outer html of DOMDocument? I'm interested in why they seem to treat HTML entities differently. EXAMPLE: function outerHTML($node) { $doc = new DOMDocument(); $doc->appendChild($doc->importNode($node, true)); return $doc->saveHTML(); } $html = '<p>ACME’s 27” Monitor is $200.</p>'; $dom = new DOMDocument(); @$dom->loadHTML($html); $el = $dom->getElementsByTagname('p')->item(0); echo $el-

Keeping line breaks when using PHP's DomDocument appendChild

邮差的信 提交于 2021-01-28 04:09:06
问题 I'm trying to use the DOMDocument in PHP to add/parse things in an HTML document. From what I could read, setting the formOutput to true and preserveWhiteSpace to false should keep the tabs and newlines in order, but it doesn't seem like it is for newly created or appended nodes. Here's the code: $dom = new \DOMDocument; $dom->formatOutput = true; $dom->preserveWhiteSpace = false; $dom->loadHTMLFile($htmlsource); $tables = $dom->getElementsByTagName('table'); foreach($tables as $table) {

PHP DOMDocument : How to parse custom XML/RSS tag names with COLONS?

安稳与你 提交于 2021-01-01 07:10:25
问题 I have the below RSS to parse, something like: <?xml version="1.0" encoding="utf-8"?> <rss xmlns:x-wr="http://www.w3.org/2002/12/cal/prod/Apple_Comp_628d9d8459c556fa#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:x-example="http://www.example.com/rss/x-example" xmlns:x-microsoft="http://schemas.microsoft.com/x-microsoft" xmlns:xCal="urn:ietf:params:xml:ns:xcal" version="2.0"> <channel> <item> <title>About Apples</title> <author>David

Extracting a specific row of a table by DOMDocument

拥有回忆 提交于 2020-08-24 08:27:45
问题 how can I extract information from a HTML file by using DOMDocument in PHP my HTML page has a source with this part inside this is my third table in the page that I need to work on: <table> <tbody> <tr> <td>A</td> <td>B</td> <td>C</td> <td>D</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> </tbody> </table> If my use ask me for showing row with B and D how should I extract the first row of this table and print it by using DOMDocument? 回答1: This would do it, it simply grabs

PHP DomDocument - why is en dash “–” converted to –

元气小坏坏 提交于 2020-05-28 05:32:25
问题 I am using DOMDocument to extract some paragraphs. Here is how my initial htm file that I am impotrting looks like: <html> <head> <title>Toxins</title> </head> <body> <p class=8reference><span>1.</span><span>Sivonen, K.; Jones, G. Cyanobacterial Toxins. In <i>Toxic Cyanobacteria in Water. A Guide to Their Public Health Consequences, Monitoring and Management</i>; Chorus, I., Bartram, J., Eds.; E. and F.N. Spon: London, UK, 1999; pp. 41–111.</span></p> </body> </html> When I am doing: $dom