Ugh. PHP dummy here sorry. Searched and searched here, but search foo not good enough. I feel like I’m dancing around an answer, but array_filter, array_search, recursive etc. I’m just not getting a handle on it mentally haha.
I have this sample from an XML export where the ordering of the XML is NOT consistent nor well formed and the nesting levels are even different at times etc. etc. so I can’t grab values based on where it is specifically in the export, which even dummy me could do:
<INFRA>
<GUID>7C32C0A2-EF31-11E9-8128-4CCC6A98779E</GUID>
<ID>501</ID>
<NAME>P Cedar</NAME>
<TYP>36</TYP>
<TYPKRZ>50_IN_36_PARKPLATZ</TYPKRZ>
<TYPNAME>parking place</TYPNAME>
<STATUS>open</STATUS>
<STATUSWERT k="SONSTIGE" v="miscellaneous">1</STATUSWERT>
</INFRA>
In this instance I need to find/search for unique string <NAME>P Cedar</NAME>
no matter where in the export it may end up. Then I need to assign a variable to the <STATUS>
of that particular <INFRA><NAME>
so I can do some operators against it. TIA!
2
Answers
Here is a solution based on DOM and XPath:
For performance reason, consider extracting all the NAME / STATUS pairs at once (no need to execute the XPath query several times).
Use
DOMXpath::evaluate()
. Xpath expressions allow for complex conditions and casts. So you logic can be put directly into an expression:INFRA
element in the document://INFRA
NAME
://INFRA[NAME]
P Cedar
://INFRA[NAME="P Cedar"]
STATUS
children://INFRA[NAME="P Cedar"]/STATUS
string(//INFRA[NAME="P Cedar"]/STATUS)
Xpath functions like
string()
take a list of nodes and use the text content of the first node (or an empty string).