简体   繁体   中英

How to output two XML documents with PHP?

I have these two documents, and in this stage I want to output both directly to browser. When I am echoing first, or second document, everything is fine, but, when I try to output them both, I got this error:

XML Parsing Error: junk after document element at location where first XML document ends ( ...</response></epp> )and second element starts ( <?xml version="1... ).

How do I output them both directly to browser? You can see source output here .

You cannot directly output two XML s, because the XML format requires only one root element, and in your case you echo two root elements. Beside that, and more important, you output twice the <?xml version="1... part, which is not allowed more than once, and is required to be present at the beginning of the document.

What you can do, if you want to return both within the same request, is to generate a third XML that aggregates the two roots by adding them as children to the root of the 3rd XML. For example if your xml's look like

<?xml version="1.0"?>
<root1>
.....
</root1>

and

<?xml version="1.0"?>
<root2>
.....
</root2>

then the agregate xml would look like this:

<?xml version="1.0"?>
<root3>
    <root1>
    .....
    </root1>

    <root2>
    .....
    </root2>
</root3>

You can use an xml parser/writer to achieve this, or you can use brute force by using string manipulation in order to combine the two xml roots.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM