powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / Java [игнор отключен] [закрыт для гостей] / Отображение XmlElement даже с пустым значением
3 сообщений из 3, страница 1 из 1
Отображение XmlElement даже с пустым значением
    #39121069
Phricker
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Доброго дня.

Есть написанный на java веб-сервис, который помимо всего прочего возвращает
Код: java
1.
@WebResult(name = "GetAgentListResult", targetNamespace = "http://test.ru/")


Код: java
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
import javax.xml.bind.annotation.*;
import java.util.ArrayList;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"getAgentListResult"}, namespace = "http://test.ru/")
@XmlRootElement(name = "GetAgentListResponse", namespace = "http://test.ru/")
public class GetAgentListResponse {

    @XmlElement(name = "GetAgentListResult")
    protected GetAgentListResponse.GetAgentListResult getAgentListResult;


    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {"agents"})
    public static class GetAgentListResult {

        @XmlMixed
        @XmlAnyElement(lax = true)
        protected ArrayList<Agent> agents;

        public ArrayList<Agent> getAgents() {
            if (agents == null) {
                agents = new ArrayList<Agent>();
            }
            return this.agents;
        }
    }
}




В котором в свою очередь

Код: java
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"agentId", "agentTitle", "agentComment", "agentAgentCode", "subList", "manager"}, namespace = "http://test.ru/")
@XmlRootElement(name = "Agent", namespace = "http://test.ru/")

public class Agent {
    @XmlElement(name = "AgentId")
    protected String agentId;
    @XmlElement(name = "AgentTitle")
    protected String agentTitle;
    @XmlElement(name = "AgentComment")
    protected String agentComment;
    @XmlElement(name = "AgentAgentCode")
    protected String agentAgentCode;
    @XmlElement(name = "SubList")
    protected String subList;
    @XmlElement(name = "Manager")
    protected String manager;


    public String getAgentId() { return this.AgentId; }

    public void setAgentId(String value) { this.AgentId = value; }

    public String getAgentTitle() { return this.AgentTitle; }

    public void setAgentTitle(String value) { this.AgentTitle = value; }

    public String getAgentComment() { return this.AgentComment; }

    public void setAgentComment(String value) { this.AgentComment = value; }

    public String getAgentAgentCode() { return this.agentAgentCode; }

    public void setAgentAgentCode(String value) { this.agentAgentCode = value; }

    public String getSubList() { return this.subList; }

    public void setSubList(String value) { this.subList = value; }

    public String getManager() { return this.manager; }

    public void setManager(String value) { this.manager = value; }
}



При тестировании через SoapUI возвращается следующая XML
Код: xml
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:GetAgentListResponse xmlns:ns2="http://test.ru/">
         <ns2:GetAgentListResult>
            <ns2:Agent>
               <AgentId>36</AgentId>
               <AgentTitle>101000015</AgentTitle>
               <AgentComment>ООО "Рога и копыта"</AgentComment>
               <AgentAgentCode>111</AgentAgentCode>
               <SubList>37</SubList>
            </ns2:Agent>
            <ns2:Agent>
               <AgentId>38</AgentId>
               <AgentTitle>101000017</AgentTitle>
               <AgentComment/>
               <AgentAgentCode>333</AgentAgentCode>
            </ns2:Agent>
            <ns2:Agent>
               <AgentId>39</AgentId>
               <AgentTitle>101000018</AgentTitle>
               <AgentComment>Lenovo Inc</AgentComment>
            </ns2:Agent>
         </ns2:GetAgentListResult>
      </ns2:GetAgentListResponse>
   </S:Body>
</S:Envelope>




Как можно сделать, чтобы в XML отображались все XmlElement'ы даже если они пустые?
В XML видно, что в одном случае вернулся SubList т.к. он заполнен, а в других двух случаях нет. И в одном случае не вернулся AgentAgentCode

Т.е. чтобы они отображались наподобии <AgentComment/> во втором примере. Но чтобы они были в XML
...
Рейтинг: 0 / 0
Отображение XmlElement даже с пустым значением
    #39121085
0FD
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Phricker,

nillable и required пробовал?
...
Рейтинг: 0 / 0
Отображение XmlElement даже с пустым значением
    #39121093
Phricker
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Пробовал.
Возвращалось
Код: xml
1.
2.
3.
4.
5.
6.
7.
8.
<ns2:Agent1C>
               <ContractId>38</ContractId>
               <ContractTitle>101000017</ContractTitle>
               <ContractComment/>
               <AgentContractCode>333</AgentContractCode>
               <SubList xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
               <Manager xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            </ns2:Agent1C>


Думал, что я накосячил.

Значения туда помещаются из результатов SQL запроса.
Сделал проверку на NULL в результатах, и получилось что хотел.

Код: xml
1.
2.
3.
4.
5.
6.
7.
8.
<ns2:Agent1C>
               <ContractId>38</ContractId>
               <ContractTitle>101000017</ContractTitle>
               <ContractComment/>
               <AgentContractCode>333</AgentContractCode>
               <SubList/>
               <Manager/>
            </ns2:Agent1C>



Спасибо за помощь!
...
Рейтинг: 0 / 0
3 сообщений из 3, страница 1 из 1
Форумы / Java [игнор отключен] [закрыт для гостей] / Отображение XmlElement даже с пустым значением
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]