DOMElement::getAttribute()
(PHP 5, PHP 7)
Returns value of attribute
说明
public DOMElement::getAttribute(string $name): string
Gets the value of the attribute with name$namefor the current node.
参数
- $name
The name of the attribute.
返回值
The value of the attribute, or an empty string if no attribute with the given$nameis found.
参见
- DOMElement::hasAttribute() Checks to see if attribute exists
- DOMElement::setAttribute() Adds new attribute
- DOMElement::removeAttribute() Removes attribute
- - - - - - - - - - - - - - XML Data: <data> <Report ID="1"> <Date>REVIEW</Date> <AuthorID>1</AuthorID> </Report> <Report ID="2"> <Date>REVIEW</Date> <AuthorID>2</AuthorID> </Report> </data> - - - - - - - - - - - - - - <?php $xmlDoc = new DOMDocument(); $xmlDoc->load( 'data.xml' ); $searchNode = $xmlDoc->getElementsByTagName( "Report" ); foreach( $searchNode as $searchNode ) { $valueID = $searchNode->getAttribute('ID'); $xmlDate = $searchNode->getElementsByTagName( "Date" ); $valueDate = $xmlDate->item(0)->nodeValue; $xmlAuthorID = $searchNode->getElementsByTagName( "AuthorID" ); $valueAuthorID = $xmlAuthorID->item(0)->nodeValue; echo "$valueID - $valueDate - $valueAuthorID\n"; } ?> - - - - - - - - - - - - - - Output: 1 - REVIEW - 1 2 - REVIEW - 2 - - - - - - - - - - - - - -