Re: [xml] Regarding Adding namespace prefix to the Root Element



Hello,

I want to generate a XML like the following , In this example root element Envelope having
namespace prefix as "S11" . How to add the namespace prefix to the Root Element. In my code below
I have simply given Root element name as "S11:Envelope", I know it is wrong, Please let me know
the correct way to do it.

<?xml version="1.0"?>
<S11:Envelope xmlns:S11="..." xmlns:wsu="..." xmlns:wsse="..." xmlns:xenc="..." xmlns:wst="...">
  <S11:Header>
    <wsse:Security>
     <wsse:UsernameToken>
        <wsse:Username>appx_username</wsse:Username>
        <wsse:Password>password</wsse:Password>
      </wsse:UsernameToken>
    </wsse:Security>
  </S11:Header>
</S11:Envelope>

I do not see anything wrong in your XML file, you do indeed need to declare the namespace and use it in the 
same element, so :

<S11:Envelope xmlns:S11="..." ...>

is right.


Code:

    doc = xmlNewDoc(BAD_CAST "1.0");
     root = xmlNewNode(NULL, BAD_CAST "S11:Envelope");
    ns = xmlNewNs(root,"...","S11");

I have never used namespaces programmatically like this, but I do not think this is how it should be done, 
the namespace prefix should not be provided in the element name. Have you tried this?

doc = xmlNewDoc(BAD_CAST "1.0");
root = xmlNewNode(NULL, BAD_CAST "Envelope");
ns = xmlNewNs(root,"...","S11");
xmlSetNs(root, ns);

Regards,
Romain

    ns1 = xmlNewNs(root,"...","wsu");
    ns2 = xmlNewNs(root,"...","wsse");
    ns3 = xmlNewNs(root,"...","xenc");
    ns4 = xmlNewNs(root,"...","wst");

    /* Create some nodes */
    node = xmlNewChild(root, ns, BAD_CAST "Header", NULL);
    node1 = xmlNewChild(node, ns2, BAD_CAST "Security", NULL);
    node2 = xmlNewChild(node1, ns2, BAD_CAST "UsernameToken", NULL);
    xmlNewChild(node2, ns2, BAD_CAST "Username", "appx_username");
    xmlNewChild(node2, ns2, BAD_CAST "Password", "password");

    xmlDocSetRootElement(doc, root);

     /* Dump the document to a buffer and print it for demonstration purposes. */
    xmlDocDumpFormatMemory(doc, &xmlbuff, &buffersize, 1);
    xml = (char*)xmlbuff;
    printf("\nThe created document:\n%s\n",xml);

_________________________________________________________________________________________________________________________

Ce message et ses pieces jointes peuvent contenir des informations confidentielles ou privilegiees et ne 
doivent donc
pas etre diffuses, exploites ou copies sans autorisation. Si vous avez recu ce message par erreur, veuillez 
le signaler
a l'expediteur et le detruire ainsi que les pieces jointes. Les messages electroniques etant susceptibles 
d'alteration,
France Telecom - Orange decline toute responsabilite si ce message a ete altere, deforme ou falsifie. Merci.

This message and its attachments may contain confidential or privileged information that may be protected by 
law;
they should not be distributed, used or copied without authorisation.
If you have received this email in error, please notify the sender and delete this message and its 
attachments.
As emails may be altered, France Telecom - Orange is not liable for messages that have been modified, changed 
or falsified.
Thank you.




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]