0

I have an XML string which consists of two xml files. (For the sake of readability I cut the below xml string):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" ...
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" ...

I want to loop through each xml document and replace the text within <a:t> tags with a variable. The code below does that and works correctly, however only for the first xml document. It doesn't continue when it comes to the second <?xml version="1.0" encoding="UTF-8" standalone="yes"?> xml declaration.

How can I loop through multiple xml declarations and execute the code below?

        $reader = new XMLReader();
        $reader->xml($extract);

        while (@$reader->read()) {
            if (@$reader->nodeType == XMLREADER::ELEMENT && $reader->name === 'a:t'){
                $i++;
                $string = @$reader->readString();
                $extract = str_replace($string, $i, $extract);
            }
        }
Stefan Schneider
  • 157
  • 3
  • 18
  • Well, 2 documents in one string makes it an invalid document. I _doubt_ that there are 'nice' ways of solving this with `xmlreader`, you might have to split those two xml documents 'by hand' into 2 valid ones, but perhaps someone else has a better idea. – Wrikken Oct 31 '14 at 09:39
  • 2
    The [*XMLReaderIterator* library](https://github.com/hakre/XMLReaderIterator) has an **XMLSequenceStream** which can deal with such type of input for **XMLReader** (and other PHP libraries which operate on streams). See as well an existing answer showing exactly that: http://stackoverflow.com/a/25233496/367456 /cc @Wrikken – hakre Nov 01 '14 at 11:08
  • 1
    If you operate on strings and you do it in memory only anyway, there is also [this answer which explains doing that with `preg_split`](http://stackoverflow.com/a/26309145/367456) for question [Loading non-standard xml wsman data into object with php](http://stackoverflow.com/q/26267043/367456). And the [**XMLRecoder** class](https://gist.github.com/hakre/5194634) also contains a pretty complete set of regular expression patters for XML Declartions which is something you might be looking for, too. – hakre Nov 01 '14 at 11:18
  • @hakre, thanks for the cc, I'll check it out! – Wrikken Nov 01 '14 at 11:55

0 Answers0