2

I'm working on soap webservices. i want to know how to display response xml with empty elements when the data is null.

Eg:

My XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="TrackInfo">
  <xs:complexType>
<xs:sequence>
  <xs:element name="Name" type="xs:string" />
  <xs:element name="Length" type="xs:string"  />
  <xs:element name="AverageSpeed" type="xs:string"/>
</xs:sequence>
  </xs:complexType>
 </xs:element>

Sample XML document:

<TrackInfo>
<Name>Barcelona</Name>
<Length>4591</Length>
<AverageSpeed>20</AverageSpeed>
</TrackInfo>

Java Code to set values

TrackInfo trackinfo= new TrackInfo();
trackinfo.setName("Barcelona");
trackinfo.setLength("4591");
trackinfo.setAverageSpeed("20");

There are cases where i may not have data to set and in that case i want to display xml with empty elements. Otherwise i need to handle null cases for huge code.

TrackInfo trackinfo= new TrackInfo();
trackinfo.setName(null);
trackinfo.setLength(null);
trackinfo.setAverageSpeed(null);

<TrackInfo>
<Name></Name>
<Length></Length>
<AverageSpeed></AverageSpeed>
</TrackInfo>

if i try with nillable=true i get response as

<TrackInfo>
<Name xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<Length xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-  instance"/>
<AverageSpeed xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</TrackInfo>

Please advise how to proceed for any other possibilities

Thanks.

2
  • i want to know how to display response xml with empty elements when the data is null ... it appears that using nillable="true" is achieving this. Then what else is the problem? Commented Jul 9, 2015 at 5:26
  • Were you able to resolve your problem? Commented Jul 11, 2015 at 1:07

2 Answers 2

0

Can you try to set the below

TrackInfo trackinfo= new TrackInfo();
trackinfo.setName("");
trackinfo.setLength("");
trackinfo.setAverageSpeed("");

The response would have empty tags.

1
  • Yes. i do the same, but i have a huge xml and i need to check for the NULL values (if null condition)for all elements. Commented Jul 9, 2015 at 5:01
0

You can add default = "" to your <xs:element> tags. Try using this XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="TrackInfo">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Name" type="xs:string" default = "" />
                <xs:element name="Length" type="xs:string" default = "" />
                <xs:element name="AverageSpeed" type="xs:string" default = "" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
6
  • if i try with nillable=true i get something like <Name xsi:nil="true" xmlns:xsi="w3.org/2001/XMLSchema-instance"> . Please check my above question, have updated the response for nillable=true Commented Jul 9, 2015 at 5:19
  • Yes but if you try to access the value of a nillable empty tag, do you get null in your Java code? From what I can tell, you will always get <tag/> for an empty tag rather than <tag></tag>. Commented Jul 9, 2015 at 5:20
  • But response xml perspective i want to display empty tags and not <Name xsi:nil="true" xmlns:xsi="w3.org/2001/XMLSchema-instance"/> Commented Jul 9, 2015 at 5:40
  • How are you rendering the tags? Is there an option to remove all attributes? Commented Jul 9, 2015 at 5:42
  • I'm doing TrackInfo trackinfo= new TrackInfo(); trackinfo.setName(""); trackinfo.setLength(""); trackinfo.setAverageSpeed(""); to make it empty tags Commented Jul 9, 2015 at 5:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.