domdocument

PHP DOMDocument::save() saves as ASCII instead of UTF-8

牧云@^-^@ 提交于 2020-01-04 05:33:27
问题 I'm using DOMDocument and SimpleXMLElement to create a formatted XML file. While this all works, the resulting file is saved as ASCII, not as UTF-8. I can't find an answer as to how to change that. The XML is created as so: $XMLNS = "http://www.sitemaps.org/schemas/sitemap/0.9"; $rootNode = new \SimpleXMLElement("<?xml version='1.0' encoding='UTF-8'?><urlset></urlset>"); $rootNode->addAttribute('xmlns', $XMLNS); $url = $rootNode->addChild('url'); $url->addChild('loc', "Somewhere over the

DOMElement does not seem to be removed from DOMNodeList

点点圈 提交于 2020-01-04 05:26:46
问题 Plase, help! Need to delete 1st element from DOMNodeList $myDivs , but actual removal dosen't occur - element remains. $dom = new DOMDocument(); $dom->loadHTML($file); $xpath = new DOMXPath($dom); $myDivs = $xpath->query('//div[@data-name|data-price]'); usleep(1); //Must REVERSE iterate DOMNodeList. for ($i = $myDivs->length - 1; $i >= 0; $i--) { //Deleting 1st element of (DOMNodeList) $myDivs, containing advertisement product if ($i == 0) { //Removing div element (DOMNode) from DOM?

Loop through xml elements and display in a table in a new row?

元气小坏坏 提交于 2020-01-03 06:39:05
问题 UPDATE: How would I get this code to display in each row in a table. I have this table so far and I've tried putting this code before and inside the <td> but it won't put the above xml file onto a new line for each <cogxml> . Can you see where I am going wrong? Test 2.php is the php code that Parfait has in the answer. <?php include_once('test2.php'); ?> <html> <head> <title>CharGer and CoGui</title> <link rel="stylesheet" type="text/css" href="../XSLT/myStyle.css" /> </head> <body> <div

Remove HTML Tag using DOMDocument

淺唱寂寞╮ 提交于 2020-01-03 03:36:06
问题 I'd like to remove <font> tags from my html and am trying to use replaceChild to do so, but it doesn't seem to work properly. Can anyone catch what might be wrong? $html = '<html><body><br><font class="heading2">Limited Size and Resources</font><p><br><strong>Q: When can a member use the limited size and resources exception?</strong></p></body></html>'; $dom = new DOMDocument(); $dom->loadHTML($html); $font_tags = $dom->GetElementsByTagName('font'); foreach($font_tags as $font_tag) { foreach(

PHP DOM traverse HTML nodes and childnode

泪湿孤枕 提交于 2020-01-03 01:40:11
问题 I am using some code to pick out all the <td> tags from a HTML page: $dom = new DOMDocument; $dom->loadHTML($html); foreach ($dom->getElementsByTagName('td') as $node) { $array_data[ ] = $node->nodeValue; } This stores the data fine in my array. The html data being looked at is: <tr> <td>DATA 1</td> <td><a href="12345">DATA 2</a></td> <td>DATA 3</td> </tr> The $array_data returns: Array([0])=>DATA 1 [1]=>DATA 2 [2]=> DATA 3) My desired output is to get code out of the <a> tag that is

getting src element using domDocument

强颜欢笑 提交于 2020-01-02 20:10:45
问题 I am using domDocument. I am close but need help for the last little bit I have this html just a snippet below. There are a number of rows. I am trying to get the href. so far i am doing the following: I can get the table, tr, and td ok , but not sure what to do from there. Thanks for any help foreach ($dom->getElementsByTagName('table') as $tableitem) { if ( $tableitem->getAttribute('class') == 'tableStyle02'){ $rows = $tableitem->getElementsByTagName('tr'); foreach ($rows as $row){ $cols =

getting src element using domDocument

蓝咒 提交于 2020-01-02 20:10:30
问题 I am using domDocument. I am close but need help for the last little bit I have this html just a snippet below. There are a number of rows. I am trying to get the href. so far i am doing the following: I can get the table, tr, and td ok , but not sure what to do from there. Thanks for any help foreach ($dom->getElementsByTagName('table') as $tableitem) { if ( $tableitem->getAttribute('class') == 'tableStyle02'){ $rows = $tableitem->getElementsByTagName('tr'); foreach ($rows as $row){ $cols =

Is there a way to get all of a DOMElement's attributes?

北战南征 提交于 2020-01-02 01:11:14
问题 I'm reading some XML with PHP and currently using the DOMDocument class to do so. I need a way to grab the names and values of a tag's (instance of DOMElement ) attributes, without knowing beforehand what any of them are. The documentation doesn't seem to offer anything like this. I know that I can get an attribute's value if I have its name, but again, I don't know either of these and need to find both. I also know that other classes like SimpleXMLElement have this capability, but I'm

Select nodeValue but exclude child elements

旧时模样 提交于 2020-01-01 05:34:19
问题 Let's say I have this code: <p dataname="description"> Hello this is a description. <a href="#">Click here for more.</a> </p> How do I select the nodeValue of p but exclude a and it's content? My current code: $result = $xpath->query("//p[@dataname='description'][not(self::a)]"); I select it by $result->item(0)->nodeValue; 回答1: Simply appending /text() to your query should do the trick $result = $xpath->query("//p[@dataname='description'][not(self::a)]/text()"); 回答2: Unsure if PHP's XPath

DOMDocument - Load xml rss - failed to open stream

余生长醉 提交于 2019-12-31 05:10:30
问题 I want to get links from a rss url . This is my code : $doc = new DOMDocument(); $doc->load("http://www.alef.ir/rssdx.gmyefy,ggeltshmci.62ay2x.y.xml"); $arrFeeds = array(); foreach ($doc->getElementsByTagName('item') as $node) { $title = $node->getElementsByTagName('title')->item(0)->nodeValue; $title=strip_tags($title); $link=$node->getElementsByTagName('link')->item(0)->nodeValue; } I've used this code for several others URLs and all of them worked but on this one I get: Warning: