Thanks in part to SO I was able to figure out how I can access XML tags with hyphens (<some-tag>). All the examples I have seen do it something like this.
$content = $xml->{'document-content'};
But for me that doesn't work, and this does
$content = $xml->{document-content};
that is without the quotes (how I figured that out I don't recall, a mistake maybe). If I use the quotes I get this error
Notice: Trying to get property of non-object in /html/my/dir/myfile.php on line 26
So one would think just use it without the quotes. Sure that is till I get to the reason I am parsing the XML. The XML is from an ODT file and will ultimately be used as template to generate PDF's. While developing anything I always use "E_ALL" error reporting. With it set I get these 2 errors when I use it without quotes.
Notice: Use of undefined constant document - assumed 'document' in /html/my/dir/myfile.php on line 24
Notice: Use of undefined constant content - assumed 'content' in /html/my/dir/myfile.php on line 24
But, it does parse the rest of the document just fine. Problem is that I need to create a PDF and if it outputs that "Notice" error prior to the PDF generator running the "header" does not get set properly and no PDF is created. Now one might suggest I turn off error reporting, but then if the PDF isn't working I can't see those errors.
In truth I am at a loss as to why it works at all without quotes. Everything I know about PHP syntax says that without quotes it would be a constant (as the error points out) that must be defined some where prior to it. As such the entire parser should fail at that point, but it doesn't, in fact the opposite is true the parser works.
Mostly I just need to know how to get rid of those 2 notice errors, without disabling error reporting. And I would be very interested in why it works without the quotes, as how it is working seems to drastically deviate from all the norms of programing.
Just in case its needed here is all the code leading up to "$content"
$zip = new ZipArchive;
if ($zip->open('../docs/myfile.odt') === true)
{
$xmlstring = $zip->getFromName('content.xml');
$zip->close();
}
// remove all namespaces and swaps out tab and space tags
$replace = array('office:', 'style:', 'draw:', 'fo:', 'text:', 'svg:', '<tab/>', '<s/>');
$value = array('', '', '', '', '', '', "\t", ' ');
$xmlstring = str_replace($replace, $value, $xmlstring);
$xmlstring = preg_replace_callback('/<s c="(.+?)"\/>/s', 'ReplaceSpaces', $xmlstring);
$xml = simplexml_load_string($xmlstring);
$content = $xml->{document-content};
I should kick myself, I have parsed a lot of XML files and RSS feeds, over the years, and the answer is very simple. You do not need to reference the main element of an XML when using SimpleXML.
<document-content>
<tag1>
</tag1>
<tag2>
<anothertag>
</anothertag>
</tag2>
</document-content>
So $xml->{document-content}->tag2
(or $xml->{0}->tag2
) is the exact same as $xml->tag2
I am guessing with this being my first go round with odt file's, and having to deal with some of its hassles, I overlooked the obvious.