For parse xml there are XML Parser, XMLReader and SimpleXML. XML Parser and XMLReader are used for big file, SimpleXML - for small files (<1Mb).
function startElement($parser, $tag, $attrs) {
global $articles, $isArticle, $i, $globTag;
$globTag = $tag;
if ($tag == 'article') {
$isArticle = true;
if (isset ( $attrs ['mdate'] )) {
// add date from attribute in article
$articles [$i] ['mdate'] = $attrs ['mdate'];
}
}
}
function endElement($parser, $tag) {
global $articles, $isArticle, $i, $globTag;
if ($tag == 'article') {
$isArticle = false;
++ $i;
}
}
function getElement($parser, $data) {
global $articles, $isArticle, $i, $globTag;
if ($isArticle) {
$articles [$i] = $articles [$i] + [
$globTag => $data
];
}
}
global $articles, $isArticle, $i, $globTag;
$articles = [ ];
$i = 0;
$isArticle = false;
$url = 'http://dblp.uni-trier.de/rec/bibtex/';
$key = 'journals/acta/BayerM72';
$url .= $key;
$parser = xml_parser_create ();
xml_set_element_handler ( $parser, "startElement", "endElement" );
xml_set_character_data_handler ( $parser, 'getElement' );
xml_parser_set_option ( $parser, XML_OPTION_CASE_FOLDING, false );
$file = fopen ( $url, 'rb' );
if ($file === false) {
die ( "File isnt!!" );
}
$clasterSize = 8192;
while ( $data = fread ( $file, $clasterSize ) ) {
if (! xml_parse ( $parser, $data, feof ( $file ) )) {
die ( sprintf ( "XML error: %s at line %d", xml_error_string ( xml_get_error_code ( $parser ) ), xml_get_current_line_number ( $parser ) ) );
}
}
xml_parser_free ( $parser );
fclose ( $file );
This is example in XML Parser.
<?php
$url = 'http://dblp.uni-trier.de/rec/bibtex/';
$key = 'journals/acta/BayerM72';
$content = file_get_contents($url . $key);
$xml = new SimpleXMLElement($content);
/* Search for <dblp><article> */
$result = $xml->xpath('/dblp/article');
// $result is an array of SimpleXMLElement objects
var_dump($result);
?>
There is SimpleXML example. You get an array of SimpleXMLElement objects in result. Look a manual to get SimpleXMLElement attributes SimpleXMLElement->attributes();
.