Обычная версия
Java форум JavaTalks
форум программистов

Поиск   Пользователи   Группы   Регистрация 
 Профиль   Личные сообщения 

 Вход 

soap&SOAPBodyElement.addAttribute
Список форумов
 ->  Java Enterprise и распределённые технологии


 
Начать новую тему 
Предыдущая тема :: Следующая тема  
Автор Сообщение
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")Wink,
то получаю
<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. Надо читать доку Sad
К началу Посмотреть профиль Отправить личное сообщение
nullvoid : 505
Постоянный посетитель
Откуда: Красноярск

СообщениеДек 29, 2011 11:44 
Ответить с цитатой
xmlns типа служебное слово, и для имен аттрибутов не годится, не?
_________________
http://LinguaLeo.ru/r/8b3o08
К началу Посмотреть профиль Отправить личное сообщение Посетить сайт автора ICQ Number
 
Начать новую тему  Ответить на тему
Страница 1 из 1
Список форумов
 -> Java Enterprise и распределённые технологии


 
Вы не можете начинать темы
Вы не можете отвечать на сообщения
Вы не можете редактировать свои сообщения
Вы не можете удалять свои сообщения
Вы не можете голосовать в опросах


Java and all Java-related trademarks and logos are trademarks or registered trademarks of Oracle Corporation in the United States and other countries.
Это сайт не относится к фирме Oracle Corporation и не поддерживается ею.

© 2006-2010 www.javatalks.ru: форум java программистов
Используется скрипт phpBB © 2001, 2010 phpBB Group

Хостинг от bizname.ru