I have some XML which contains a lot of information in the attributes, here is a small example.
<?xml version="1.0" encoding="UTF-8"?>
<collection xmlns="http://www.loc.gov/MARC21/slim">
<record>
<leader>04170npc a22003613u 4500</leader>
<controlfield tag="001">vtls003932502</controlfield>
<controlfield tag="003">WlAbNL</controlfield>
<datafield tag="035" ind1=" " ind2=" ">
<subfield code="a">(WlAbNL)1002</subfield>
</datafield>
<datafield tag="040" ind1=" " ind2=" ">
<subfield code="a">WlAbNL</subfield>
<subfield code="b">eng</subfield>
<subfield code="c">WlAbNL</subfield>
</datafield>
<datafield tag="245" ind1="0" ind2="0">
<subfield code="a">Scott Blair Collection,</subfield>
<subfield code="f">1910 -</subfield>
</datafield>
<datafield tag="653" ind1=" " ind2=" ">
<subfield code="a">rheology</subfield>
</datafield>
</record>
<record>
<leader>04229npc a22005893u 4500</leader>
<controlfield tag="001">vtls003932503</controlfield>
<datafield tag="035" ind1=" " ind2=" ">
<subfield code="a">(WlAbNL)1004</subfield>
</datafield>
<datafield tag="040" ind1=" " ind2=" ">
<subfield code="a">WlAbNL</subfield>
<subfield code="b">eng</subfield>
<subfield code="c">WlAbNL</subfield>
</datafield>
<datafield tag="245" ind1="0" ind2="0">
<subfield code="a">Celtic Collection,</subfield>
<subfield code="f">17th century -</subfield>
</datafield>
<datafield tag="653" ind1=" " ind2=" ">
<subfield code="a">Scottish Gaelic language</subfield>
</datafield>
</record>
</collection>
Currently I have a php script which just loads the entire document
$xml = simplexml_load_file("Mapping_coll_wales.xml");
$records = $xml->record;
This creates a records array which looks something like this (i have cut this down a bit to one record)
SimpleXMLElement Object
(
[leader] => 04170npc a22003613u 4500
[controlfield] => Array
(
[0] => vtls003932502
[1] => WlAbNL
)
[datafield] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[tag] => 035
[ind1] =>
[ind2] =>
)
[subfield] => (WlAbNL)1002
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[tag] => 040
[ind1] =>
[ind2] =>
)
[subfield] => Array
(
[0] => WlAbNL
[1] => eng
[2] => WlAbNL
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[tag] => 245
[ind1] => 0
[ind2] => 0
)
[subfield] => Array
(
[0] => Scott Blair Collection,
[1] => 1910 -
)
)
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[tag] => 653
[ind1] =>
[ind2] =>
)
[subfield] => rheology
)
)
)
Currently im just pulling the field I need by presuming where in the array it is, and looping over each record (there are about 500)
for ($i =0; $i <5; $i++) {
echo '<strong>Title</strong> = : ' . $records[$i]->datafield[2]->subfield . '<br />';
echo '<strong>tag</strong> = :' . $records[$i]->datafield[3]->subfield . '<br />';
echo '<br />------------------------------------------------------------------------<br />';
}
However its possible that the xml may contain other tags, so I dont want to rely on it being the subfield of indices 2 etc. Ideally I would like to be able to call it using something like
echo '<strong>Title</strong> = : ' . $records[$i]->datafield[245][a] . '<br />';
Im sure its fairly straight forward and im just missing something, but it would be good to be able to either load the tags as the array indices or have some way of directly getting datafield by its tag and the subfield by its code, as that wont change.
Hope that makes sense.
Paul