Php: Domelement->Getattribute

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

  1. I believe (object value omitted) is just some internal DOM or var_dump() restriction, to prevent dumping too deep and/or dumping recursive information about the object graph.

  2. Then, about getting information about attributes:

    • To get all attributes of a DOMElement, you would use the attributes property, which is defined on its parent class DOMNode and returns a DOMNamedNodeMap with DOMAttr 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 a DOMElement, you could use:

      • DOMElement::getAttributeNode(), to get aDOMAttr node that represents the type 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 attribute type, 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:

  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 " ';

php DOM getAttribute

The problem is the ul has text nodes as children along with the lis 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 lis 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



Leave a reply



Submit