kzsnyk
kzsnyk

Reputation: 2211

QXmlStreamWriter, namespace and prefix

I am new to XML and trying to write the following xml using QXmlStreamWriter :

<S:Envelope xmlns:S="url1">
        <S:Body>
                <ns2:name1 xmlns:ns2="url2">
                ..

So far I have tried:

sw.writeNamespace("url1", "S");
sw.writeStartElement("url1", "Envelope");
sw.writeStartElement("url1", "Body");

sw.writeStartElement("url2", "name1");
sw.writeNamespace("url2", "ns2");

but the result is not what I expect:

 <S:Envelope xmlns:S="url1">
            <S:Body>
                    <n1:name1 xmlns:n1="url2">
                    ..

As specified in the documentation, the defaut prefix n1 is used instead of ns2.

If I swap the last 2 lines, I have:

<S:Envelope xmlns:S="url1">
            <S:Body xmlns:ns2="url2">
                    <ns2:name1>
                    ..

What am I doing wrong?

Upvotes: 1

Views: 302

Answers (1)

scopchanov
scopchanov

Reputation: 8399

From the documentation of QXmlStreamWriter:

you can bypass the stream writer's namespace support and use overloaded methods that take a qualified name instead.

That is, use this overloaded method: QXmlStreamWriter::writeStartElement.

I suggest you to modify your code like that:

sw.writeNamespace("url1", "S");

sw.writeStartElement("url1", "Envelope");
sw.writeStartElement("url1", "Body");

sw.writeStartElement("ns2:name1");
sw.writeNamespace("url2", "ns2");

This produces the result you have stated as desired:

<S:Envelope xmlns:S="url1">
    <S:Body>
        <ns2:name1 xmlns:ns2="url2">

Upvotes: 2

Related Questions