How to parse complicated nested XML in a JSON-like format?
STOP.
I know what you're going to say.
$response = $guzzleClient->get($url);
$contents = $response->getBody()->getContents();
$xml = simplexml_load_string($contents, \SimpleXMLElement::class, LIBXML_NOCDATA);
$parsedXml = json_decode(json_encode($xml), true);
This doesn't work. Here are a couple of the API endpoints I'm trying to parse data from (this is an API for looking up medical studies, basically, it's called the Entrez E-Utilities API): Searching by article title, Getting details on a few articles by ID
Check those out to get an idea of the structure of the XML I'm trying to deal with. If you try to run the results of the second link through the above recommended code, it produces a jumbled mess of arrays that is unusable. The publication date, article title, authors, etc., all the info I need is keyed numerically, so I have no way to know what is what. I've found this SO page with a function for parsing nested XML: https://stackoverflow.com/a/64654236/8619523
I tried a slightly modified version of that function. The result is better I guess, but very unwieldy. Here's what it looks like as a json string, toss that in your preferred browser-based json viewer. Not very user-friendly, is it?
{"Tag":"eSearchResult","Value":"","Children":[{"Tag":"Count","Value":"2977"},{"Tag":"RetMax","Value":"20"},{"Tag":"RetStart","Value":"0"},{"Tag":"IdList","Value":"","Children":[{"Tag":"Id","Value":"36277635"},{"Tag":"Id","Value":"36263752"},{"Tag":"Id","Value":"36258488"},{"Tag":"Id","Value":"36250702"},{"Tag":"Id","Value":"36245092"},{"Tag":"Id","Value":"36222849"},{"Tag":"Id","Value":"36222585"},{"Tag":"Id","Value":"36212680"},{"Tag":"Id","Value":"36210474"},{"Tag":"Id","Value":"36209566"},{"Tag":"Id","Value":"36205238"},{"Tag":"Id","Value":"36202314"},{"Tag":"Id","Value":"36201284"},{"Tag":"Id","Value":"36197517"},{"Tag":"Id","Value":"36194093"},{"Tag":"Id","Value":"36173929"},{"Tag":"Id","Value":"36172737"},{"Tag":"Id","Value":"36148976"},{"Tag":"Id","Value":"36136409"},{"Tag":"Id","Value":"36134138"}]},{"Tag":"TranslationSet","Value":""},{"Tag":"TranslationStack","Value":"","Children":[{"Tag":"TermSet","Value":"","Children":[{"Tag":"Term","Value":"\"hetero\"[TITL]"},{"Tag":"Field","Value":"TITL"},{"Tag":"Count","Value":"2977"},{"Tag":"Explode","Value":"N"}]},{"Tag":"OP","Value":"GROUP"}]},{"Tag":"QueryTranslation","Value":"\"hetero\"[TITL]"}]}
Does anyone have a good way to really get a simple json-like PHP array from XML like this? I would've thought this was simple and routine, I've never worked with XML before. But it seems way more complicated than I expected.
Please or to participate in this conversation.