PHP Domdocument Getting Attribute of Tag

PHP DOMDocument getting Attribute of Tag

The example you provided should not generate an error. I tested it and $linkthumb contained the string "example text string" as expected

Ensure the media namespace is defined in the returned XML otherwise DOMDocument will error out.

If you are getting a specific error, please edit your post to include it

Edit:

Try the following code:

$xmldoc = new DOMDocument();
$xmldoc->load('api response address');
foreach ($xmldoc->getElementsByTagName('item') as $feeditem) {
$nodes = $feeditem->getElementsByTagName('file');
$linkthumb = $nodes->item(0)->getAttribute('data');
echo $linkthumb;
}

You may also want to look at SimpleXML and Xpath as it makes reading XML much easier than DOMDocument.

PHP: DomElement-getAttribute

"Inspired" by Simon's answer. I think you can cut out the getAttribute call, so here's a solution without it:

$attrs = array();
for ($i = 0; $i < $a->attributes->length; ++$i) {
$node = $a->attributes->item($i);
$attrs[$node->nodeName] = $node->nodeValue;
}
var_dump($attrs);

PHP DOMDocument: Get attribute value from id

Try this :

$data->getAttribute('value'); 

PHP: DomElement->getAttribute

$attrs = array();
for ($i = 0; $i < $data->attributes->length; ++$i){
$node = $data->attributes->item($i);
$attrs[$node->nodeName] = $node->nodeValue;
}
var_dump($attrs);

Change tag attribute value with PHP DOMDocument

$dom = new DOMDocument();
$dom->loadHTML('<a href="http://foo.bar/">Click here</a>');

foreach ($dom->getElementsByTagName('a') as $item) {

$item->setAttribute('href', 'http://google.com/');
echo $dom->saveHTML();
exit;
}

php DOMDocument()-getAttribute() not working

In php Sandbox your code works.

However, you forgot < at the beginning of a tag.

<?php
$string = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
</head>
<body onclick="on_body_click()" text="#000000" alink="#FF0000" link="#0000FF" vlink="#800080">
<a href="/cgi-bin/new_get_recorded.cgi?l_doc_ref_no=7506389&COUNTY=san francisco&YEARSEGMENT=current&SEARCH_TYPE=DETAIL_N" title="Document Details">Show Name Detail</a>
</body>
</html>';

$doc = new DOMDocument();
$doc->loadHTML($string);
$selector = new DOMXPath($doc);
$result = $selector->query('//a[@title="Document Details"]');
$url = $result[0]->getAttribute('href');
echo $url;

In $url you have the href value (printed out).

It seems you have problem with the string and use of ' and ".

If you start the $string with ' you cannot use it inside. You can use ' just at the end to close the php variable ';;

You have three solutions:

  1. substitute ' with " inside the string representing your html;
  2. Use \' instead of only ' inside the string representing your html. This tells to php that the string is not finished yet but that ' represents the string content;
  3. heredoc syntax;

For example with the first approach we have:

$string = ' Inside the string you should use just this type of apostrophe " ';

DOM structure, get element by attribute name/value

I suggest and you could use an DOMXpath in this case too. Example:

$url = "https://plus.google.com/+Mcgowansac/about";
$curl = curl_init($url);
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');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);

$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($output);
libxml_clear_errors();
$xpath = new DOMXpath($dom);

$review = $xpath->query('//div[@guidedhelpid="userreviews"]');

if($review->length > 0) { // if it exists
echo $review->item(0)->nodeValue;
// echoes
// John DeRemer reviewed 3 months ago Last fall, we had a major issue with mold which required major ... and so on
}

PHP getting and setting attributes on HTML Elements

// to get the 'src' attribute
$src = $frame->getAttribute('src');

// to set the 'src' attribute
$frame->setAttribute('src', 'newValue');

To change the URL, you should first use parse_url($src), then rebuild it with your new query arguments, for example:

$parts = parse_url($src);
extract($parts); // creates $host, $scheme, $path, $query...

// extract query string into an array;
// be careful if you have magic quotes enabled (this function may add slashes)
parse_str($query, $args);
$args['newArg'] = 'someValue';

// rebuild query string
$query = http_build_query($args);

$newSrc = sprintf('%s://%s%s?%s', $scheme, $host, $path, $query);

PHP: getAttribute() all attributes

You can use the public $attributes property from the DOMNode element that the DOMElement inherits from. See also here in the documentation: DOMNode::$attributes

Code example:

$document = <<<DOCUMENT
<div id='one' style='width: 10px'></div>
DOCUMENT;

$document = DOMDocument::loadXML($document);
$element = $document->getElementsByTagName('div')->item(0);

$attributes = $element->attributes;
for ($i = 0; $i < $attributes->length; $i++) {
$item = $attributes->item($i);
echo 'There is an attribute called: "' . $item->nodeName . '" with value: ' . $item->nodeValue . PHP_EOL;
}

See also the eval.in that I created: https://eval.in/494934

php DOMDocument: how can I print an attribute of an element?

use DOMElement::getAttribute

$art->getAttribute('class');

also, simpleHTMLDOM is more suitable for dealing with html:

$html = str_get_html($page);
foreach($html->find('td') as $element)
echo $element->class.'<br>';
}


Related Topics



Leave a reply



Submit