I’m trying to access specific parts of a XML file with PHP (simplexml) without looping through. I just want to access a specific field. But how? This is the structure of the xml file:
<collection xmlns="http://www.loc.gov/MARC21/slim">
<record>
<datafield tag="010" ind1=" " ind2=" ">
<subfield code="a">96174405</subfield>
</datafield>
<datafield tag="035" ind1=" " ind2=" ">
<subfield code="a">(OCoLC)ocm34158924</subfield>
</datafield>
</record>
</collection>
For example, how would I print 96174405?
print_r ($collection->record->datafield[0]->subfield);
That doesn’t seem to work. How do I account for the attributes?
Thanks!
Cast to string when using
print_r():The SimpleXML extension objects are created dynamically.
Clarification:
If you don’t cast to string,
print_r()will display this instead:Thus the recommendation.