Re: Problems working with HTML using PHP's XML tools (placing mixed text/html into xpath-specified nodes...)
| From: | Michael A. Peters | Date: | Thu, 21 May 2009 19:25:29 +0000 |
| Subject: | Re: Problems working with HTML using PHP's XML tools (placing mixed text/html into xpath-specified nodes...) | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-292902@lists.php.net to get a copy of this message | ||
Weston C wrote:
Is there a straightforward way (or, heck, any way) of placing mixed html/text content into xpath-specified nodes using any of PHP's XML tools? So far, I've tried SimpleXML and the DOM and things aren't coming out well.Not sure if it is of any use to you, I don't use XPath at all, but this php class modifies existing DOMDocument objects for filtering purposes and may give you an idea of how it is done: http://www.clfsrpm.net/xss/cspfilter_class.phps For a clearer example of adding a node to an existing document - from http://www.clfsrpm.net/xss/dom_script_test.phps That page is hard to read as it was butchered from another script, so here are the demonstrative parts: $import_prepare = '<div>' . $somexmlcontentinastring . '</div>'; $newDom = new DOMDocument("1.0","UTF-8"); $newDom->loadXML($import_prepare); $elements = $newDom->getElementsByTagName("div"); $imported_div = $elements->item(0); Now - $imported_div is an object node containing all the xml in import_prepare (you don't have to put it in a div, I did there because it's a form so I don't know the structure of xml input, putting it in a div gave me a known element that would be parent of all input) - but it is a node for the $newDom DOMDocument, I need to import it into the DOMDocument I am using. $testDiv = $myxhtml->importNode($imported_div,true); Now, $testDiv an a DOM object associated with the DOMDocument I want to import the node into. $xmlBody->appendChild($testDiv); Now the node is a child of the $xmlBody node, and has been incorporated into my DOMDocument. -=-=-=- importHTML() is problematic with some characters. Better to convert your additions to well formed xml and use importXML(); -=-=-=- While I am currently using DOMDocument for everything (yes, everything), I'm still learning it - do not consider me a guru on it's use.