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);
How to get Attributes from DOMElement
I believe
(object value omitted)
is just some internalDOM
orvar_dump()
restriction, to prevent dumping too deep and/or dumping recursive information about the object graph.Then, about getting information about attributes:
To get all attributes of a
DOMElement
, you would use theattributes
property, which is defined on its parent classDOMNode
and returns aDOMNamedNodeMap
withDOMAttr
nodes:// $this->node must be a DOMElement, otherwise $attributes will be NULL
$attributes = $this->node->attributes;
// then, to loop through all attributes:
foreach( $attributes as $attribute ) {
// do something with $attribute (which will be a DOMAttr instance)
}
// or, perhaps like so:
for( $i = 0; $i < $attributes->length; $i++ ) {
$attribute = $attributes->item( $i );
// do something with $attribute (which will be a DOMAttr instance)
}
// to get a single attribute from the map:
$typeAttribute = $attributes->getNamedItem( 'type' );
// (which will be a DOMAttr instance or NULL if it doesn't exist)To get just one attribute, named
type
from aDOMElement
, you could use:DOMElement::getAttributeNode()
, to get aDOMAttr
node that represents thetype
attribute, like so:$typeAttr = $this->node->getAttributeNode( 'type' );
// (which will be NULL if it doesn't exist)or
DOMElement::getAttribute()
, to get the string value of the attributetype
, like so:$typeAttrValue = $this->node->getAttribute( 'type' );
// (which will an empty string if it doesn't exist)
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 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 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:
- substitute
'
with"
inside the string representing your html; - 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; - heredoc syntax;
For example with the first approach we have:
$string = ' Inside the string you should use just this type of apostrophe " ';
php DOM getAttribute
The problem is the ul
has text nodes as children along with the li
s textnodes do not have attribute therefore you get an error. just test if the child is an element node before you try to access its attribute
foreach ($items as $item) {
if ($item->nodeType == XML_ELEMENT_NODE)
echo $item->getAttribute('data-text');
}
You can also use getElementsByTagName()
, although if you have nested lists the li
s in them will also be selected.
$items = $list->getElementsByTagName('li');
foreach ($items as $item) {
echo $item->getAttribute('data-text');
}
DOMElement, how to (do a simple) check if an attribute exists?
See DOMElement::hasAttribute
if ($node->hasAttribute('class')) {do somthing};
Related Topics
Mysqli Commands Out of Sync; You Can't Run This Command Now
Passing an Instance Method as Argument in PHP
Any PHP Code to Detect the Browser with Version and Operating System
Processing Multidimensional JSON Array with PHP
Convert Hex to Ascii Characters
Set Httponly and Secure on PHPsessid Cookie in PHP
How to Check Whether Suhosin Is Installed
Regular Expression: Find Range Except for One Letter or a Range
Php: What Is the Complexity [I.E O(1),O(N)] of the Function 'Count'
Why Does an Infinitely Recursive Function in PHP Cause a Segfault
How to Check If File Is Ascii or Binary in PHP
PHP Function to Replace a (I)Th-Position Character
How to Have Multiple $_Get with the Same Key, Different Values
How to Tell Apart Simplexml Objects Representing Element and Attribute