Accessing same name child nodes in XML

FinalDemise

New Member
I am having a problem with a simple read of Child nodes in an XML file.The first few lines of code presented here are for background only anddo not form the basis for the query.The object of the code is to end up with two arrays, mrdarr and nrdarrthat contain, respectively "ROAD 2", "ROAD 4", "ROAD 6", "ROAD 8"and "ROAD 1", "ROAD 3", "ROAD 5", "ROAD 7", "ROAD 9".The contents will be used to populate a MySQL table further on. I have tried many different combinations of code, all to no avail. Allthe forum problem solutions I have seen are for different variations of the same thing. Most of them use XPath, which I want to avoid for this code, as it is a one-off.Any advice appreciated.\[code\]<?xml version="1.0" encoding="utf-8" ?> <root> <state> <name>State name</name> <abbr>ABBR</abbr> <major_roads> <road>ROAD 2</road> <road>ROAD 4</road> <road>ROAD 6</road> <road>ROAD 8</road> </major_roads> <minor_roads> <road>ROAD 1</road> <road>ROAD 3</road> <road>ROAD 5</road> <road>ROAD 7</road> <road>ROAD 9</road> </minor_roads> </state></root>$objDOM = new DOMDocument(); $objDOM->load("example.xml"); $state = $objDOM->getElementsByTagName("state"); foreach( $state as $value ){ $oname = $value->getElementsByTagName("name"); $onam = $oname->item(0)->nodeValue; $oabbr = $value->getElementsByTagName("abbr"); $oabb = $oabbr->item(0)->nodeValue;}...$mrdarr = array();$j = 0; $mroads = $objDOM->getElementsByTagName("major_roads"); foreach( $mroads as $value ){ $mrdes = $value->getElementsByTagName("road"); $mrdarr[$j] = $nrdes->ChildNodes.nodeValue; $j++;} $nrdarr = array();$j = 0; $nroads = $objDOM->getElementsByTagName("minor_roads"); foreach( $nroads as $value ){ $nrdes = $value->getElementsByTagName("road"); $nrdarr[$j] = $nrdes->ChildNodes.nodeValue; $j++;}\[/code\]
 
Back
Top