|
Java форум JavaTalks форум программистов
|
|
|
|
| Предыдущая тема :: Следующая тема |
| Автор |
Сообщение |
FOONTIK : 2 Новичок
|
Дек 29, 2011 11:16 |
|
|
Не удается добавить атрибут вида xmlns="http://ws.creditinfo.com".
В результате выполнения кода:
| Код: |
// set up the factories and create a SOAP factory
SOAPConnection soapConnection = SOAPConnectionFactory.newInstance().createConnection();
SOAPFactory soapFactory = SOAPFactory.newInstance();
MessageFactory messageFactory = MessageFactory.newInstance();
// Create a message from the message factory.
SOAPMessage soapMessage = messageFactory.createMessage();
// creat a SOAP part have populate the envelope
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.setEncodingStyle( SOAPConstants.URI_NS_SOAP_ENCODING );
// remove all header information from envelope
envelope.getHeader().detachNode();
// create a SOAP body
SOAPBody body = envelope.getBody();
// add the element <ns1:BabelFishRequest> to the body
// in axis RC1, have to do this instead of body.addChildElement( ... )
Name babelFishRequestName = envelope.createName( "BabelFish");
SOAPBodyElement soapMethod = body.addBodyElement( babelFishRequestName );
soapMethod.addAttribute(soapFactory.createName("xmlns") ,"http://ws.creditinfo.com");
// add elements translationmode and sourcedata to BabelFishRequest
soapMethod.addChildElement( soapFactory.createElement( "translationmode" ).addTextNode( "en_fr" ) );
soapMethod.addChildElement( soapFactory.createElement( "sourcedata" ).addTextNode( "hello" ) );
// set the saves into the structure
soapMessage.saveChanges();
// output the message
System.out.println( "\n============= start request msg ==========\n" );
soapMessage.writeTo( System.out );
System.out.println( "\n============= end request msg ==========\n" );
// close down the connection
soapConnection.close(); |
получаю пусто:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<BabelFish xmlns="">
<translationmode>en_fr</translationmode>
<sourcedata>hello</sourcedata>
</BabelFish>
</SOAP-ENV:Body>
Если меняю "xmlns" на любое другое, например "xxx"
(soapMethod.addAttribute(soapFactory.createName("xxx") ,"http://ws.creditinfo.com") ,
то получаю
<BabelFish xxx="http://ws.creditinfo.com">.
Куда смотреть, подскажите, плз? |
|
|
|
 |
FOONTIK : 2 Новичок
|
Дек 29, 2011 11:44 |
|
|
Спасибо всем! Ответ найден. Надо делать так.
| Код: |
// add the element <ns1:BabelFishRequest> to the body
// in axis RC1, have to do this instead of body.addChildElement( ... )
/*Name babelFishRequestName = envelope.createName( "BabelFish");
SOAPBodyElement soapMethod = body.addBodyElement( babelFishRequestName );
soapMethod.addAttribute(soapFactory.createName("xxx") ,"http://ws.creditinfo.com");*/
QName bodyName = new QName("http://ws.creditinfo.com","BabelFish", "");
SOAPBodyElement soapMethod = body.addBodyElement(bodyName);
// add elements translationmode and sourcedata to BabelFishRequest
/*soapMethod.addChildElement(soapFactory.createElement( "translationmode" ).addTextNode( "en_fr" ) );
soapMethod.addChildElement(soapFactory.createElement( "sourcedata" ).addTextNode( "hello" ) );*/
QName name = new QName("translationmode");
SOAPElement symbol = soapMethod.addChildElement(name);
symbol.addTextNode("en_fr");
name = new QName("sourcedata");
symbol = soapMethod.addChildElement(name);
symbol.addTextNode("hello"); |
<BabelFish xmlns="http://ws.creditinfo.com">
<translationmode>en_fr</translationmode>
<sourcedata>hello</sourcedata>
</BabelFish>
Получается, что xmlns="http://ws.creditinfo.com" не просто атрибут, скорее всего это зарезервированное слово xmlns под namespace. Надо читать доку  |
|
|
|
 |
nullvoid : 505 Постоянный посетитель Откуда: Красноярск
|
Дек 29, 2011 11:44 |
|
|
|
|
|
|
 |
|
|
|