-->

KSOAP2 how to change Prefixes

2019-08-10 19:39发布

问题:

I got little Problems with my generated code:

here the XML Structure that i need to have:

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<soapenv:Envelope xmlns:v1="http://openclinica.org/ws/study/v1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="soapenc" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
<wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken wsu:Id="UsernameToken-27777511" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:Username>xxxx</wsse:Username>
<wsse:Password type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">xxxxxxxx</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<v1:listAllRequest/>
</soapenv:Body>
</soapenv:Envelope>

and here´s my Code that i have done:

declarePrefix("v1", NAMESPACE);
getBody().addTextNode(null, METHOD_NAME, "?");
XMLParentNode security = new XMLParentNodeImpl(HEADER_NAMESPACE, "Security");
security.addAttribute(HEADER_NAMESPACE,"mustUnderstand", "1");
XMLParentNode usernameToken = new XMLParentNodeImpl(UNT_NAMESPACE, "UsernameToken");
usernameToken.addTextNode(null, "wsse:Username", "root");
usernameToken.addTextNode(PASSTYPE, "wsse:Password", Constants.OC_PASSWORD);
security.addElement(usernameToken);
getHeader().addElement(security);

but the output looks like:

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<soapenv:Envelope xmlns:v1="http://openclinica.org/ws/study/v1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
<n0:Security n0:mustUnderstand="1" xmlns:n0="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<n1:UsernameToken xmlns:n1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:Username>xxxx</wsse:Username>
<n2:wsse:Password xmlns:n2="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">xxxxxxxx</n2:wsse:Password>
</n1:UsernameToken>
</n0:Security>
</soapenv:Header>
<soapenv:Body>
<listAllRequest>?</listAllRequest>
</soapenv:Body>
</soapenv:Envelope>

you see i have some trouble with the lines

<n0:Security....
<n1:UsernameToken...
<n2:wsse:Password...

how can i change the n0,n1 etc... not just in the beginn of the line specialy on the tags like n0:mustUnderstand="1"

i tryed to declare prefixes etc but nothing changes tis object.

Can someone tell with what method i can change this things Thanks a lot for helping

Christian

回答1:

for all intrestet Users, i solved it that way:

declarePrefix("v1", NAMESPACE);
        declarePrefix("soapenc", "soapenc");
        getBody().addTextNode(null, METHOD_NAME, "?");
        XMLParentNode security = new XMLParentNodeImpl(null, "wsse:Security");
        security.declarePrefix("wsse", HEADER_NAMESPACE);
        security.addAttribute(HEADER_NAMESPACE,"mustUnderstand", "1");
        XMLParentNode usernameToken = new XMLParentNodeImpl(null, "wsse:UsernameToken");
        usernameToken.declarePrefix("wsu", UNT_NAMESPACE);
        usernameToken.addAttribute(null, "wsu:Id", "UsernameToken-27777511");
        usernameToken.addTextNode(null, "wsse:Username", Constants.OC_Username);
        usernameToken.addTextNode(null, "wsse:Password", Constants.OC_PASSWORD);
        security.addElement(usernameToken);
        getHeader().addElement(security);

the results not 100% simular to my outgoing code, but the request are working :)