$dom = new DOMDocument; $dom->loadHTML($content); $images = $dom->getElementsByTagName('img'); foreach ($images as $image) { $node = $dom->createElement("div"); $frag = $dom->createDocumentFragment(); // create fragment $frag->appendXML('<div class="test"><a href="#">some link</a><span>test</span></div>'); $node.appendChild($frag); $image->parentNode->replaceChild($node, $image); } It's HTML. The dynamic nature of HTML makes it very difficult to perform regex or string replacement...
You can use ->length property of the DOMNodelist before pointing out on that item: $item_content = ''; if($x->item($i)->getElementsByTagName('encoded')->length > 0) { // if it exists, or at least one $item_content = $x->item($i)->getElementsByTagName('encoded')->item(0)->nodeValue; } ...
You actually don't need to load the SimpleXML object into that json_encode/decode. You can use that object already and parse whatever values you needed. Having to encode/decode as an array and to have to access values and then converting it into SimpleXML is too much. $xml = '<?xml version="1.0"?> <step...
php,html,parsing,xpath,domdocument
XPath is not guilty at all. Span tags are added dinamically. Just have a look at the source code of the page, not the DOM-Structure, which may be already modified by javascript, but use "view-source:" and you will see exactly the same html, as it is parsed by XPath. It...
AYou should check whether your query yielded any elements (DOMNodelist). Check it first then get the element. $elements = $xpath->query('//span[@id="ProductName"]'); if($elements->length > 0) { echo $elements->item(0)->nodeValue; } Sidenote: cant test this though im on mobile but this should be the basic idea...
This is solution for my problem $terms = get_the_terms( $post->ID, 'product_cat' ); $typ = get_option('product_type'); $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' ); if ( $wccolor ){ $category = $dom->createElement("category", $wccolor); } else { $category = $dom->createElement("category", $typ); } $product->appendChild($category); } ...
xml,excel,vba,domdocument,ixmldomelement
You XML parser ignores the whitespace node. Try changing the DOMDocument.preserveWhiteSpace property before loading the XML. Dim xmlDoc As MSXML2.DOMDocument Set xmlDoc = New MSXML2.DOMDocument xmlDoc.preserveWhiteSpace = true xmlDoc.LoadXML aboveXMLStringVale HINT: You're using the XML parser to parse SOAP. Do you don't have SOAP parser/library available?...
php,html,xpath,xhtml,domdocument
Alternatively, you could just point out the row itself, then filter them out using that ->tagName: $dom = new DomDocument; libxml_use_internal_errors(true); $dom->loadHTML($html); libxml_clear_errors(); $xpath = new DomXPath($dom); $rows = $xpath->query('/html/body/table[2]/tr'); foreach ($rows as $row) { foreach($row->childNodes as $col) { if(isset($col->tagName) && $col->tagName != 'th') { echo $col->textContent . '<br/>'; }...
Yes you can continue to use xpath to traverse the elements on the header and get its following sibling, the list. Example: $doc = new DOMDocument(); $doc->loadHTML($output); $xpath = new DOMXpath($doc); $elements = $xpath->query('//div[@class="outer"]/div'); if($elements->length > 0) { foreach($elements as $div) { foreach ($xpath->query('./h1', $div) as $e) { $header =...
php,html,table,preg-replace,domdocument
Use DOMDocument. $table = <<< EOD <table> <tbody> <tr style="border:none;"> <td>1</td> <td>[[name_21]]</td> <td>[[charge_2]]</td> </tr> <tr style="border:1px solid #aaa;"> <td>2</td> <td>[[name_13]]</td> <td>[[charge_5]]</td> </tr> <tr style="border:1px solid #ccc;"> <td>3</td> <td>[[name_24]]</td> <td>[[charge_13]]</td> </tr> <tr style="border:1px solid...
DOMDocument has no method asXML(). You need to use save(): $dom->save("vocab.xml"); ...
The issue is that you're just sending it the URL not the Page itself, use either file_get_contents or cURL and then send it to your DOM document. E.G $xml = file_get_contents("http://api.openweathermap.org/data/2.5/weather?lat=".$latitude."&lon=".$longitude); $xmlDoc3 = new DOMDocument(); $xmlDoc3->load($xml); ...
php,xml,special-characters,domdocument
You will have to create the text node and append it. I described the problem in this answer: http://stackoverflow.com/a/22957785/2265374 However you can extend DOMDocument and overload createElement*(). class MyDOMDocument extends DOMDocument { public function createElement($name, $content = '') { $node = parent::createElement($name); if ((string)$content !== '') { $node->appendChild($this->createTextNode($content)); } return...
php,image,domdocument,fopen,src
src isn't a tag, it's an attribute. You said you're new to php so that's pretty normal, now worries, use this code: $doc = new DOMDocument(); $doc->loadHTMLFile("https://www.flickr.com/search/?text=arushad%20ahmed"); $xpath = new DOMXpath($doc); $imgs = $xpath->query("//img"); for ($i=0; $i < $imgs->length; $i++) { $img = $imgs->item($i); $src = $img->getAttribute("src"); // do something...
php,xml,domdocument,getelementsbytagname
Is this what you after? $htmlStr = '<td colspan=3> <p class=5tablebody> <span style=\'position:relative;top:14.0pt\'> <img width=300 height=220 src="forMerrin_files/image020.png"> </span> </p> </td>'; $doc = new DOMDocument(); $doc->loadHTML($htmlStr); $paragraphs = $doc->getElementsByTagName('img'); var_dump($paragraphs->item(0)->getAttribute('src')); Outputs: string 'forMerrin_files/image020.png' (length=28) ...
If you want to count elements with the local name of xyz, the expression should be as follows: XPathExpression xpr = xpath.compile("count(//*[local-name()='xyz']/*[local-name()='a']/*[local-name()='b']/*[local-name()='c'])"); Demo....
I think you're using the getElementsByTagName() the wrong way. This method is used to get HTML tags, not their attributes or value of attributes. Please check the following example: <?php $html = '<a id="test" href="#">Some link text here</a>'; $html .= '<a id="test2" href="#">Some link text here</a>'; $DOM = new DOMDocument();...
Here's the same example using DOMDocument: $doc = new DOMDocument; $list = $doc->appendChild($doc->createElement('ul')); $list->setAttribute('id', 'nav'); foreach ($navArr as $name => $path) { $listItem = $list->appendChild($doc->createElement('li')); if (in_array($URI, $path)) { $listItem->setAttribute('class', 'active'); } $link = $listItem->appendChild($doc->createElement('a')); $link->setAttribute('href', $path[1]); $link->appendChild($doc->createTextNode($name)); } return $doc->saveHTML();...
You can use an XPath expression to find <strong>Palabras: </strong> then the first following sibling text node that doesn't consist entirely of whitespace. Example: $xpath = new DOMXPath($doc); $query = '//strong[.="Palabras: "]/following-sibling::text()[normalize-space()][1]'; foreach ($xpath->query($query) as $node) { echo $node->nodeValue; } ...
Since you have to skip the first element of the list, a for loop may be better. The code below totals the first column in the rows. $total = 0; $trs=$tables->item(0)->getElementsByTagName('tr'); for ($rownum = 1; $rownum < $trs->length; $rownum++) { $row = $trs->item($rownum); $td = $row->getElementsByTagName('td')->item(0); $total += $td->textContent; }...
It works for me: $div = $xpath->query('//*[@class="prodImg"]')->item(0)->getElementsByTagName('img')->item(0)->getAttribute('src'); var_dump($div); Yields: string 'some_image_src' (length=14) ...
php,xml,wordpress,rss,domdocument
You can use this way : $feed = new DOMDocument(); $feed->load('http://www.revolutionpersonaltraining.com.au/blog/feed/'); $items = array(); foreach ($feed->getElementsByTagName('item') as $item) { array_push($items, array ( 'title' => $item->getElementsByTagName('title')->item(0)->nodeValue, 'desc' => $item->getElementsByTagName('description')->item(0)->nodeValue, 'link' => $item->getElementsByTagName('link')->item(0)->nodeValue, 'date' =>...
php,html,dom,domdocument,domxpath
Do it like this: <?php $xpath = new DOMXPath($dom); $spans = $xpath->query('//table[@id="tbvalue"]//img[1]'); echo $spans->item(0)->getAttribute("src"); // operator means to select nodes in the document from the current node that match the selection no matter where they are More useful information you can find here....
Problem #1 Based on your markup, you're trying to target <td> tags, but in your query, it's //div, which doesn't make sense. Target <td>'s: $rows = $xpath->query('//tr/td[@class = "abc pqr xyz"]'); foreach($rows as $b){ echo $b->nodeValue . '<br/>'; } Sample Output Problem #2 This is most likely related to this...
As noted in http://www.php.net/manual/en/domdocument.createelement.php The value will not be escaped. Use DOMDocument::createTextNode() to create a text node with escaping support With that you can try: $doc = new DomDocument(); $scriptContent = 'return "undefined" !== typeof b **&&** null !== b ? b.getAttribute("content") : 0'; $scriptElement = $doc->createElement('script'); $scriptElement->appendchild($doc->createTextNode($scriptContent)); $doc->appendchild($scriptElement); echo...
try this : $img = $item->getElementsByTagName('enclosure')->item(0)->attributes->getNamedItem('url')->value; ...
php,parsing,html-parsing,domdocument
You can pass an asterisk * with getElementsByTagName() which returns all elements: foreach($dom->getElementsByTagName('*') as $element ){ } From the Manual: name The local name (without namespace) of the tag to match on. The special value * matches all tags. ...
$dom = new DOMDocument(); $dom->loadHTML($html); $xpath = new DOMXpath($dom); foreach ($xpath->evaluate('//input[@name = "guesstext2"]') as $input) { $input->parentNode->removeChild($input); } ...
Thanks for the reply. In the end it was far from the actual XML or XML code. I had a != comparator instead of !== which made it act like this. So next time I compare some variable to false value I'll probably remember that I should use !==. Small...
Here a two possible solutions: Import From A Source Document This works only if the XML string is a valid document. You need to import the document element, or any descendant of it. Depends on the part you would like to add to the target document. $xml = "<child>text</child>"; $source...
php,xml,coding-style,simplexml,domdocument
It will be difficult to read and maintain if you, or better yet -- another developer, ever need to have nested elements and multiple children.
You were definitely on the right track with you first attempt. I could spot two small problems though: As you may have guessed, you tried to output a DOMNodeList object, which is what you will get from a call to getElementsByTagName. As the name implies, it is not a single...
xml,dom,domdocument,insertafter
Okay, stupid me. The easy solution is to just go down in the DOM to the nextSibling of the nextSibling and do the same insertBefore... so this is solved.
php,html,dom,domdocument,domxpath
You just instantiate the DOMDocument then use ->loadHTML() to actually load the HTML markup: $dom = new domDocument(); libxml_use_internal_errors(true); $dom->loadHTML($html); // this line is important $xpath = new domXPath($dom); $nodes = $xpath->query('//span[@id="verbatim"]'); echo $nodes->item(0)->nodeValue; Sample Output ->evaluate() will also work as well: echo $xpath->evaluate('string(//span[@id="verbatim"])'); ...
oop,model-view-controller,structure,pip,domdocument
Just use the documentation, that's what I did, and I never used this framework before: The View is the information that is being presented to a user. A View will normally be a web page, but can be any type of "page". See http://gilbitron.github.io/PIP/#views as well. It's the controller: class...
You've already got a <c:comp-filter/>, just use setAttribute() to give it the attribute. Example: $doc = new DOMDocument('1.0', 'utf-8'); $doc->formatOutput = true; $query = $doc->createElement('c:calendar-query'); $query->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:c', 'urn:ietf:params:xml:ns:caldav'); $query->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:d', 'DAV:'); $prop = $doc->createElement('d:prop'); $prop->appendChild($doc->createElement('d:getetag'));...
You can use xpath to target that <p> tag which has a preceding sibling of div that has an ID of intsect: $doc = new DOMDocument; @$doc->loadHTMLFile("stathtml/" . $file); $xpath = new DOMXpath($doc); $p = $xpath->query('//p[preceding-sibling::div[@id="intsect"]]'); if($p->length > 0) { echo $p->item(0)->nodeValue; } Sample Output...
If I understand you correctly you would like to check for the last element in each car element node? Well, Xpath hast two methods position() and last() that can be used in a condition. Select the car nodes /details/car Select the child element nodes of the car nodes /details/car/* Add...
A regex is not the correct tool for this job. Stick with the DOM parser approach. Here's a quick solution using DOMDocument class. Use getElementsByTagName('*') to get all the tags, and then use in_array() to check if the tag name is in the list of disallowed tags. Then use a...
Yes, you can use a query like //url/loc[contains(., "other-text")] Example: $xml = <<<'XML' <root> <url> <loc>some-text</loc> </url> <url> <loc>some-other-text</loc> </url> </root> XML; $dom = new DOMDocument(); $dom->loadXML($xml); $xpath = new DOMXPath($dom); foreach ($xpath->query('//url/loc[contains(., "other-text")]') as $node) { echo $dom->saveXML($node); } Output: <loc>some-other-text</loc> ...
php,regex,preg-replace,preg-match,domdocument
You just need to change it as: $str = 'My long <a href="http://example.com/abc" rel="link">string</a> has any <a href="/local/path" title="with attributes">number</a> of <a href="#anchor" data-attr="lots">links</a>.'; $dom = new DomDocument(); $dom->loadHTML($str); $output = array(); foreach ($dom->getElementsByTagName('a') as $item) { $output[] = array ( 'str' => $dom->saveHTML($item), 'href' => $item->getAttribute('href'), 'anchorText' => $item->nodeValue...
Many ways to skin this cat. I'd give the string a dummy root node, ditch all nodes matching the xpath expression /root/blockquote/text() | /root/blockquote/*, then rebuild the string from the root's children. Example: $string = <<<'STRING' <p> This is some text </p> <p> This is some text </p> <p> This...
You can use an XPath expression to achieve this: //tr[starts-with(@id, "link")] Example: $dom = new DOMDocument; $dom->loadHTML($html); $xpath = new DOMXPath($dom); $nodes = $xpath->query('///tr[starts-with(@id, "link")]'); foreach ($nodes as $node) { // Do whatever } Demo...
On the second foreach, just target that $node->nodeName, then on the inner foreach target each id an title. foreach($xpath->evaluate('//response/*') as $node) { $tag = $node->nodeName; $params = $xpath->evaluate("//$tag/*"); foreach($params as $child) { $id = $xpath->evaluate('string(./id)', $child); $title = $xpath->evaluate('string(./title)', $child); echo $tag ." = " .$id ." = " .$title...
For one, the second iteration in the loop is overriding the values of your array "$values" which is why you'd only be seeing the values from the second product node (if you are inspecting the "$values" array which is what I'm assuming you are doing). Try this: $xmlDoc = new...
php,html,parsing,xpath,domdocument
Since you are using php you can first get all dates and iterate over those dates to get the items according to this (untested) //../node[contains(@class,'item') and preceding-sibling::node[contains(text(),'12-12-2012')]] with 12-12-2012 as the searched value....
Try: foreach($lst as $iframe) { var_dump($document->saveHtml($iframe)); } Works only with PHP >= 5.3.6 Otherwise: foreach($lst as $iframe) { $cloned = $iframe->cloneNode(TRUE); $newdoc = new DOMDocument(); $newdoc->appendChild($newdoc->importNode($cloned,TRUE)); var_dump($newdoc->saveHTML()); } ...
javascript,html,dom,browser,domdocument
I'm assuming this is in a page like this: <html> <head> <script>//that stuff</script> </head> .... In this case, the document has not yet been "closed", as it is still in the process of loading. Hence, it shouldn't clear the rest of it....
php,xml,xml-parsing,domdocument
The imdb, year, plot, and poster parts are XML attributes, not elements, so use DOMElement::getAttribute() rather than getElementsByTagName(): $imdb = $e->getAttribute('imdbID'); $year = $e->getAttribute('year'); $plot = $e->getAttribute('plot'); $poster = $e->getAttribute('poster'); ...
From https://github.com/pode/reiseplanlegger/blob/master/api/dbpedia.php: rdf: <rdf:RDF><rdf:Description rdf:about="http://dbpedia.org/resource/John_Frandsen_(footballer)"><dbpprop:placeOfBirth rdf:resource="http://dbpedia.org/resource/Denmark"/><dbpedia-owl:birthPlace rdf:resource="http://dbpedia.org/resource/Denmark"/></rdf:Description>/rdf:RDF> code: // Fetch the URL and select all rdf:Description elements. // (Note that | is the CSS 3 equiv of colons for namespacing.) // To add the context, we...
If you are referring to the name attribute value <input name="name_here" /> <!-- name attribute --> Then $node->nodeName is not what you are looking for. Use ->getAttribute() instead: if($node->getAttribute('name') == '__RequestVerificationToken'){ $vartoken = $node->nodeValue; } ...
If you want to change the value of href="" attribute, its ->setAttribute(): $link->setAttribute('href', $href); ...
php,arrays,xpath,html-parsing,domdocument
You have some small bugs in your code, see adjustments below: $allFeeds = Array ( 0 => Array ( 'feed_image' => Array ( 0 => '<a href="http://52.1.47.143/photo/928/2_onclick_ok/userid_244/" class=" js_photo_item_928 photo_holder_image" rel="928" ><img src="http://52.1.47.143/file/pic/photo/2015/04/9bd387c6442135834298d6a17b3f9555_240.jpg" alt="" width="180" height="160" class="photo_holder" /></a><br />', 1 => '<a href="http://52.1.47.143/photo/927/8/userid_244/" class=" js_photo_item_928...
javascript,jquery,html,dom,domdocument
Try to add script in head instead of title and you should use text property if your want to add some variable to it like, var script = document.createElement('script'); script.text = "var a = 1" ; //-----^ it should be text not src, if you want external js then use...
To get all href attributes of the hyperlinks, add some more axis steps, finally loop over the result list, where the ->value property will contain the URIs. Given you can just dump all href attributes inside the whole <li> element, simply extend your query by //a/@href: $document = new DOMXPath($dom);...
Most likely you're appending on the parent element instead of the form. Try to target the form first, then making the append. public function input($name, $attributes = array(), $type = 'text') { $dom = new DOMDocument(); $dom->loadXML($this->doc->saveHTML()); // target the form $form = $dom->getElementsByTagName('form')->item(0); $input = $dom->createElement('input'); $input->setAttribute('type', $type); $input->setAttribute('name',...
The title content is empty because $article_titles is still a DOMNodeList. Since you are getting elements by tag name you would expect this will return one or more element as opposed to getting elements by id, you would just be expecting just one since they are supposed to be unique....
php,xml,xslt,xampp,domdocument
Turns out my code is valid, it just requires newer versions of PHP. Uploaded my project to a free host (with the most recent version of PHP) and it worked like a charm.
Class attributes are a little special, because they are token lists (several class names seperated by whitespace), However I suggest you use Xpath to fetch the nodes: $html = <<<'HTML' <div class="titre foo">abc</div> <span class="titre bar">def</span> <div class="bar">hij</div> HTML; $dom = new DOMDocument; $dom->loadHTML($html); $dom->preserveWhiteSpace = false; $xpath = new...
php,xml,validation,schema,domdocument
The element <foo> that you create is "missing" the namespace and therefore in the null-namespace. The namespace is also the part you see in the curly (or angle) brackets in the error message: {http://www.example.com}foo `----------------------´`-´ namespace name Instead of createElement use createElementNS and provide the namespace next to the element...
UPDATE Seems you did want a flat list so im adding this specific example so there is no confusion: $html = '<div class="par"> <p class="pp"> <span class="dv">1 </span>Blah blah blah blah. <span class="dv">2 </span> Yada yada yada yada. <span class="dv">3 </span>Foo foo foo foo. </p> </div> <div class="par"> <p class="pp">...
php,html,curl,xpath,domdocument
There is nothing wrong with your xpath query as it is correct syntax and the node does exist. The problematic line is this: @$doc2->load($result2); // DOMDocument::load — Load XML from a file You are not loading the result page that you got from your curl request properly. To load the...
Using the code above failed for me and it was not due to the comma as I commented. I found that, using curl, I was able to retrieve the xml file. $c=curl_init('http://www.alef.ir/rssdx.gmyefy,ggeltshmci.62ay2x.y.xml'); curl_setopt( $c, CURLOPT_USERAGENT,'nginx-curl-blahblahblah' ); curl_setopt( $c, CURLOPT_RETURNTRANSFER, true ); $r=curl_exec( $c ); curl_close( $c ); $doc = new...
Use DOMXPath to search for existing element: $dom = new DOMDocument(); $xpath = new DOMXPath($dom); // create category element $categories = $dom->createElement('categories'); // add each element to $categories foreach ($arr as $parent => $child) { // try to find pre-existing parent $tmp_parent = $xpath->query($parent, $categories); // ..or create new parent...
php,dom,domdocument,getelementsbytagname,getattribute
Well, I suppose that you want something like this: <?php $html_fragment = <<<HTML <html> <head> <title></title> </head> <body> <div id="container"> <a href="#a">there is n image here</a> <a href="#b"><img src="path/to/image-b" alt="b: alt content"></a> <a href="#c"><img src="path-to-image-c"></a> <a href="#d"><img src="path-to-image-d" alt="c: alt content"></a> </div> </body> </html> HTML; $dom = new...
You can't remove DOMNodes from a DOMNodeList as you're iterating over them in a foreach loop (http://php.net/manual/en/domnode.removechild.php#90292). Though, making a queue of items to remove seems to work: <?php $html = <<<HTML <ul> <li><a href="/foo/bar1">link1</a></li> <li><a href="/foo/bar2">link2</a></li> <li><a href="/foo/bar3">link3</a></li> </ul> HTML; $dom = new DOMDocument; @$dom->loadHTML($html); $domNodeList =...
It's not telling you there is an empty text node there because there isn't a text node there - that simple. The xpath expression to pick out that book would be: /books/book[not(sep/preceding-sibling::text())] Example: $xml = <<<'XML' <?xml version="1.0" encoding="UTF-8"?> <books> <book>first title<sep/>first author</book> <book>second title<sep/>second author</book> <book>third title<sep/>third author</book> <book><sep/>fourth...
java,xml,rest,nodes,domdocument
Just reading the javadocs: getAttributes NamedNodeMap getAttributes() A NamedNodeMap containing the attributes of this node (if it is an *Element*) or null otherwise. The Document interface represents the entire HTML or XML document. Conceptually, it is the root of the document tree. null.getNamedItem("Amount") ??? Maybe break the steps apart?...
Found it! @$doc->loadHTML(mb_convert_encoding($str, 'HTML-ENTITIES', 'UTF-8')); This answer explains the issue and gives the work around above; DOMDocument::loadHTML will treat your string as being in ISO-8859-1 unless you tell it otherwise. This results in UTF-8 strings being interpreted incorrectly. ...
javascript,jquery,dom,domdocument
To understand ready(), you need to know about DOMContentLoaded event. The DOMContentLoaded event is fired when the document has been completely downloaded and parsed, all the external scripts and internal scripts are downloaded and executed and DOM is ready to start manipulating. This event will not wait for stylesheets, images,...
Short answer: Well, the error you're getting is due to the fact that $doc is not a DOMDocument object but it's the boolean false. Since you're suppressing DOMDocument warnings, you can't know why getHTML() is returning false. So, lose the @ operator, check what DOMDocument is complaining about and debug...
javascript,jquery,coffeescript,domdocument
In CoffeeScript, calling a function with arguments does not requires parentheses. For example: console.log("Hello") // Hello console.log "Hello" // Hello So, consider that these are equivalent: $document = $(document) $document = $ document $document = ($ document) However, parentheses are, under certain circumstances, necessary to disambiguate meaning. For example, you...
php,xml,xpath,domdocument,domxpath
Your second XPath expression is incorrect. It tries to select a div in the context of the div you selected previously as its child node. You are trying to select: //div[@id='informacio']/div[@id='trip-detail-question'] and that node does not exist. You want this node: //div[@id='informacio']/div/div[@id='trip-detail-question'] which you can also select like this (allowing...
php,domdocument,getelementsbytagname
I think you're really close The following will do the trick: $string = '<li>CIs = <a href="http://localhost/itop/web/pages/UI.pdomhp?operation=details&class=FunctionalCI&id=49&c[menu]=ConfigManagementOverview" title="Functional CI::49">Sep Console04</a>, 42 49, Sep11, Sep Console04<br/><a href="http://localhost/itop/web/pages/UI.php?operation=details&class=FunctionalCI&id=50&c[menu]=ConfigManagementOverview" title="Functional CI::50">Sep Console05</a>, 42 50, Sep11,...
The document and sample code you provided works fine in Java 1.8u25: import static org.junit.Assert.*; import java.io.IOException; import java.io.StringReader; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.junit.Test; import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class FatalErrorTest { @Test public void as_given() throws SAXException, IOException, ParserConfigurationException { String xml ="<?xml version=\"1.0\"...
php,xml,character-encoding,domdocument
I got a workaround to this problem. I bet that isn't the better way, but worked for me for now. Just complete the loadXML with this code: $xmlToParse->loadXML(mb_convert_encoding($strHotelDetailResult->textContent,'UTF-8',mb_detect_encoding($strHotelDetailResult->textContent))); ...
Solved. I found out that the problem wasn't on the class DOMDocument but yes on the function that allows the user to download the file. Previously I had this: header('Content-Description: File Transfer'); header('Content-Type: text/xml'); header('Content-Disposition: attachment; filename=' . basename($file)); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); readfile($file);...
If your intent is to remove the whole <song> element. Use ->removeChild(): $songs = $doc->getElementsByTagName('song'); $hated_artists = array('AA', 'BB', 'CC'); foreach($songs as $song) { $artist = $song->getElementsByTagName('artist')->item(0)->nodeValue; if(in_array($artist, $hated_artists)) { // if this song is sang by one of your hated artists $song->parentNode->removeChild($song); // remove this song } } Sample...
The document you're trying to load is not valid HTML and thus not valid DOM (see http://validator.w3.org/check?verbose=1&uri=http%3A%2F%2Fforums.heroesofnewerth.com%2Fshowthread.php%3F553261 for an extensive list of HTML errors on that page). So PHP basically has to guess what's meant by the HTML it's provided with and warns about that (it might guess wrong). The...
php,html,xpath,web-scraping,domdocument
First, you forgot to instantiate the DOMDocument class, (at least on the code you have in this question). $curl = curl_init('http://www.flipkart.com/apple-iphone-5s/p/itmdv6f75dyxhmt4?pid=MOBDPPZZDX8WSPAT'); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); $page = curl_exec($curl); if(!empty($curl)){ //if any html is actually returned $pokemon_doc = new DOMDocument; libxml_use_internal_errors(true);...
php,content-management-system,domdocument,read-write
DOMDocument only use to read XML and HTML, these have a structure. When you insert PHP code into html file, it is not realy a html anymore. Let see an example below. The html code: <a>text</a> There is a node that named "a" have a content. DOMDocument can understand it...
A Nice Chance to Explore some Regex Features! With all the disclaimers about using regex to work with xml-type documents... There are several interesting options for such a task. Option 1: Plain but Reliable $replaced = preg_replace('%(<jot.*?</jot>)</jot>%', '$1', $yourstring); Here, for safety, we match your whole string including the two...
php,xpath,domdocument,domxpath
Thanks to Viper-7, biberu and salathe in the ##php IRC I have this working now using: public function getTrustPilotReviews($amount) { $context = stream_context_create(array('ssl' => array('verify_peer' => false))); $url = 'https://www.trustpilot.co.uk/review/purplegriffon.com'; $data = file_get_contents($url, false, $context); libxml_use_internal_errors(true); $doc = new \DOMDocument(); $doc->loadHTML($data); $xpath = new DOMXpath($doc); $reviews = new Collection;...
Your $classname is misleading/confusing, it doesn't contain a class name which is inside your sample markup but a css style rules. $classname = "font-family:'Open Sans',arial;font-size:11px!important;color:#ccc;"; You should be searching nodes which has a style of that rule: $results = $xpath->query("//*[@style=\"" . $classname . "\"]"); Sample Output...
If you have checked the manual, there's this ->replaceChild() method you can use to replace td to th tags: $html = '<table> <thead> <tr> <td>Column 1</td> <td>Column 2</td> <td>Column 3</td> </tr> </thead> <tbody> <tr> <td>Column 1</td> <td>Column 2</td> <td>Column 3</td> </tr> <tr> <td>Column 1</td> <td>Column 2</td> <td>Column 3</td> </tr> <tr>...
Currently, you're pointing/appending to the node list instead of that found parent node: $list->appendChild($person); // ^ DOMNodeList You should point to the element: $list->item(0)->appendChild($person); Sidenote: The text can already put inside the second argument of ->createElement(): $name = $xml->createElement("name", 'Simple name again'); ...
Just remove that htmlentities(). It will work just fine. $contents = file_get_contents('http://jsonblob.com/api/jsonBlob/54a7ff55e4b0c95108d9dfec'); $obj = json_decode($contents); $dom = new DOMDocument; libxml_use_internal_errors(true); $dom->loadHTML($obj->data->partial); libxml_clear_errors(); foreach ($dom->getElementsByTagName('a') as $node) { echo $dom->saveHTML($node) . '<br/>'; echo $node->getAttribute('href') . '<br/>'; } ...
php,xml,xml-parsing,xml-serialization,domdocument
Providing the empty string second argument to createElement() adds an empty textnode to the element node. The element is not empty an can not be optimized. Without the argument DOM optimizes the XML. $dom = new DOMDocument(); $dom->appendChild($dom->createElement('root')); echo $dom->saveXml(); Output: <?xml version="1.0"?> <root/> Here is an option for saveXml()...
You cannot add items to DOMNodeList via it's public interface. However, DOMNodeLists are live collections when connected to a DOM Tree, so adding a Child Element to a DOMElement will add an element in that element's child collection (which is a DOMNodeList): $doc = new DOMDocument(); $nodelist = $doc->childNodes; //...
php,class,domdocument,autoload,spl-autoload-register
class Test2 is within namespace Test, so in order to do new Test2() you must be within the namespace Test or you can specify the fully qualified name (ie new Test\Test2()) to instantiate the class. When you call $this->dom->registerNodeClass('DOMElement', 'Test2');, DOMDocument does something to the affect of: $extendedClass = 'Test2';...