I'm generating XML from InDesign and would like to parse the XML in PHP. Below is a sample of the XML that InDesign is generating:
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<page title="About Us">
About Us
<page>Overiew</page>
<page>Where We Started</page>
<page>Help</page>
</page>
<page>
Automobiles
<page>
Cars
<page>Small</page>
<page>Medium</page>
<page>Large</page>
</page>
<page>
Trucks
<page>Flatbet</page>
<page>
Pickup
<page>Dodge</page>
<page>Nissan</page>
</page>
</page>
</page>
</Root>
I'm using the following PHP code to parse the XML recursively.
header('Content-type: text/plain');
function parse_recursive(SimpleXMLElement $element, $level = 0)
{
$indent = str_repeat("\t", $level); // determine how much we'll indent
$value = trim((string) $element); // get the value and trim any whitespace from the start and end
$attributes = $element->attributes(); // get all attributes
$children = $element->children(); // get all children
echo "{$indent}Parsing '{$element->getName()}'...".PHP_EOL;
if(count($children) == 0 && !empty($value)) // only show value if there is any and if there aren't any children
{
echo "{$indent}Value: {$element}".PHP_EOL;
}
// only show attributes if there are any
if(count($attributes) > 0)
{
echo $indent.'Has '.count($attributes).' attribute(s):'.PHP_EOL;
foreach($attributes as $attribute)
{
echo "{$indent}- {$attribute->getName()}: {$attribute}".PHP_EOL;
}
}
// only show children if there are any
if(count($children))
{
echo $indent.'Has '.count($children).' child(ren):'.PHP_EOL;
foreach($children as $child)
{
parse_recursive($child, $level+1); // recursion :)
}
}
echo $indent.PHP_EOL; // just to make it "cleaner"
}
$xml = new SimpleXMLElement('data.xml', null, true);
parse_recursive($xml);
The issue that I'm having is that when I parse the XML, I'm not getting the text values of each page node unless completely surrounded by a page tag. So, for example, I have no way of reading "About Us" unless looking at the title attribute (if it exists). The same applies for "Automobiles" and "Cars" and "Trucks".
Again, this is generated XML from InDesign. I could ask designers to add attributes to nodes, etc. but I'm trying to minimize the amount of data entry.
I believe the XML is well formed. Any help would be greatly appreciated.