Jump to content

[SOLVED] iterate xml childs


photo

Recommended Posts

I need to parse an xml file containing lots of lists like this:

 

<root>
<node arg="whatever">
  <subnode ...>
  <subnode ...>
</node>
<node arg="whatever">
  <subnode ...>
  <subnode ...>
  <subnode ...>
</node>
....
</root>

 

I have figured out how to parse children and its arguments and data, but I havent found how to iterate all the children. Can somebody help me with this?

Link to comment

Xml x = new Xml();

x.load(<xml.file>);

int num_childs = x.getNumChilds();

for( int i=0; i<num_childs; i++ )
{
   Xml xc = x.getChild(i);

   .....
}

delete x;

 

If you need some more examples for proper Xml class usage just use file search within UNIGINE distribution /data subfolder for files *.h containing 'Xml' string.

Link to comment
  • 2 years later...


<?xml version="1.0" encoding="utf-8"?>

<CountryManager>

<MapColors>

<Color>0.00 0.25 1.00 1.00</Color>

<Color>0.00 0.35 0.00 1.00</Color>

<Color>0.00 0.00 0.30 1.00</Color>

<Color>0.00 0.75 0.00 1.00</Color>

<Color>0.00 0.65 0.85 1.00</Color>

</MapColors>

<CountryNeighbours>

<Neighbours codeISO3="ABW" colorIndex="0"/>

<Neighbours codeISO3="AFG" colorIndex="3">TJK|TKM|UZB|CHN|PAK|IRN</Neighbours>

<Neighbours codeISO3="AGO" colorIndex="2">COG|COD|ZMB|NAM</Neighbours>

<Neighbours codeISO3="AIA" colorIndex="0"/>

<Neighbours codeISO3="ALB" colorIndex="0">MNE|KOS|MKD|GRC</Neighbours>

<Neighbours codeISO3="ALD" colorIndex="0"/>

<Neighbours codeISO3="AND" colorIndex="0">FRA|ESP</Neighbours>

<Neighbours codeISO3="ARE" colorIndex="0">OMN|SAU</Neighbours>

<Neighbours codeISO3="ARG" colorIndex="3">BOL|PRY|CHL|BRA|URY</Neighbours>

<Neighbours codeISO3="ARM" colorIndex="1">GEO|AZE|TUR|IRN</Neighbours>

............

</CountryNeighbours>

</CountryManager>


/**

* Load country manager data from XML file

*/

void loadXML( string filename )

{

// open XML file

Xml xml = new Xml();

 

if( xml.load( filename ) == 0 )

{

log.error("CountryManager::loadXML(): Could not open country XML file '%s' !\n", filename);

return;

}

 

int xmlModified = 0;

 

// read optional map color table

Xml xmlColors = xml.find("MapColors");

 

if( xmlColors != NULL )

{

// load color table

int index = 0;

 

for( int i=0; i<xmlColors.getNumChilds(); i++ )

{

Xml xmlColor = xmlColors.getChild(i);

 

if( xmlColor.getName() != "Color" )

{

log.error("CountryManager::loadXML(): <Color> tag expected !\n");

continue;

}

 

m_colorTable[ index++ ] = xmlColor.getVec4Data();

}

}

else

{

// store color table

Xml xmlColors = xml.addChild("MapColors");

 

for( int i=0; i<m_colorTable.size(); i++ )

{

Xml xmlColor = xmlColors.addChild("Color");

 

xmlColor.setVec4Data( m_colorTable );

}

 

xmlModified = 1;

}

 

// read country neighbouring information

Xml xmlNeighbours = xml.find("CountryNeighbours");

 

if( xmlNeighbours != NULL )

{

// read country neighbours information

for( int i=0; i<xmlNeighbours.getNumChilds(); i++ )

{

Xml xmlNeighbour = xmlNeighbours.getChild(i);

 

if( xmlNeighbour.getName() != "Neighbours" )

{

log.error("CountryManager::loadXML(): <Neighbours> tag expected !\n");

continue;

}

 

string countryISO3 = xmlNeighbour.getArg("codeISO3");

 

Country countryA = getCountry( countryISO3 );

 

if( countryA == NULL )

{

log.error("CountryManager::loadXML(): Could not find country for ISO3 code '%s' !\n", countryISO3);

continue;

}

 

countryA.border.color = xmlNeighbour.getIntArg("colorIndex");

 

string neighboursISO3[0];

 

if( strsplit( xmlNeighbour.getData(), "|", neighboursISO3 ) > 0 )

{

foreach( string neighbourISO3; neighboursISO3 )

{

Country countryB = getCountry( neighbourISO3 );

 

if( countryB == NULL )

{

log.error("CountryManager::loadXML(): Could not find country for ISO3 code '%s' !\n", neighbourISO3);

continue;

}

 

countryA.border.addNeighbour( countryB.border );

countryB.border.addNeighbour( countryA.border );

}

}

}

}

else

{

// update country neighbours information and store to xml

Xml xmlNeighbours = xml.addChild("CountryNeighbours");

 

foreach( Country country; m_countries )

{

CountryRegion border = country.border;

 

string text = "";

 

for( int i=0; i<border.getNeighbourCount(); i++ )

{

CountryRegion neighbour = border.getNeighbour( i );

 

if( text == "") text = neighbour.country.ISO3;

else text += "|" + neighbour.country.ISO3;

}

 

Xml xmlCountry = xmlNeighbours.addChild("Neighbours", format("codeISO3=%s colorIndex=%d", country.ISO3, border.color ) );

 

xmlCountry.setData(text);

}

 

xmlModified = 1;

}

 

if( xmlModified )

{

xml.save( filename );

}

}

 

Link to comment
×
×
  • Create New...