Hey,
I've never really used the dom parser before and now I have a question.
How would I go about extracting the url from this
<files>
<file path="http://www.thesite.com/download/eysjkss.zip" title="File Name" />
</files>
Thanks
Hey,
I've never really used the dom parser before and now I have a question.
How would I go about extracting the url from this
<files>
<file path="http://www.thesite.com/download/eysjkss.zip" title="File Name" />
</files>
Thanks
Using simpleXML:
$xml = new SimpleXMLElement($xmlstr);
echo $xml->file['path']."\n";
Output:
http://www.thesite.com/download/eysjkss.zip
you can use PHP Simple HTML DOM Parser,this is a php library。http://simplehtmldom.sourceforge.net/
To do it with DOM you do
$dom = new DOMDocument;
$dom->load( 'file.xml' );
foreach( $dom->getElementsByTagName( 'file' ) as $file ) {
echo $file->getAttribute( 'path' );
}
You can also do it with XPath:
$dom = new DOMDocument;
$dom->load( 'file.xml' );
$xPath = new DOMXPath( $dom );
foreach( $xPath->query( '/files/file/@path' ) as $path ) {
echo $path->nodeValue;
}
You can fetch individual nodes also by traversing the DOM manually
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->load( 'file.xml' );
echo $dom->documentElement->firstChild->getAttribute( 'path' );
Marking this CW, because this has been answered before multiple times (just with different elements), including me, but I am too lazy to find the duplicate.