j2me and ksoap How to get specific element in asp.net webservice - java

I am using ksoap with j2me. I am passing the user name and password to asp.net web service method and the web service returns the dataset to me. My problem is how can I
get Get “Display Name” element from this ksoap response (data set)?
<?xml version="1.0" encoding="utf-8" ?>
- <DataSet xmlns="http://tempuri.org/">
- <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
- <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
- <xs:complexType>
- <xs:choice minOccurs="0" maxOccurs="unbounded">
- <xs:element name="Users">
- <xs:complexType>
- <xs:sequence>
<xs:element name="Userid" type="xs:string" minOccurs="0" />
<xs:element name="Password" type="xs:string" minOccurs="0" />
<xs:element name="DisplayName" type="xs:string" minOccurs="0" />
<xs:element name="Role" type="xs:string" minOccurs="0" />
<xs:element name="Status" type="xs:string" minOccurs="0" />
<xs:element name="SessionId" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
- <xs:element name="UserAccess">
- <xs:complexType>
- <xs:sequence>
<xs:element name="Refno" type="xs:long" minOccurs="0" />
<xs:element name="UserId" type="xs:string" minOccurs="0" />
<xs:element name="MenuId" type="xs:string" minOccurs="0" />
<xs:element name="Control" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
</DataSet>

You can set
envelope.implicitTypes = true
and navigate the Soap Objects using the SoapObject approach, like this:
SoapObject list = (SoapObject)response;
for(int i=0; i < list.getPropertyCount(); i++) {
SoapObject obj = (SoapObject)list.getProperty(i);

Related

Eclipse Java - Jaxb2 plugin and WSDL

Hi I have to generate a WSDL. I have this xsd schema
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.ima.eu/xml/cattolica"
targetNamespace="http://www.ima.eu/xml/cattolica" elementFormDefault="qualified">
<xs:element name="CreateRequestInput">
<xs:complexType>
<xs:sequence>
<xs:element name="customer" type="xs:string" minOccurs="1"/>
<xs:element name="user" type="xs:string" minOccurs="1"/>
<xs:element name="company" type="xs:string" minOccurs="1"/>
<xs:element name="insuranceNumber" type="xs:string"/>
<xs:element name="number" type="xs:string"/>
<xs:element name="creationDate" type="xs:string" minOccurs="1"/>
<xs:element name="answer" type="xs:string"/>
<xs:element name="answerType" type="xs:string"/>
<xs:element name="description" type="xs:string" minOccurs="1"/>
<xs:element name="idquintuple" type="xs:string" minOccurs="1"/>
<xs:element name="status" type="xs:string"/>
<xs:element name="priority" type="xs:string"/>
<xs:element name="idOTRS" type="xs:string" minOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="CreateRequestOutput">
<xs:complexType>
<xs:sequence>
<xs:element name="CreateRequest" type="tns:CreateRequest"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="CreateRequest">
<xs:sequence>
<xs:element name="idCRM" type="xs:string"/>
<xs:element name="status" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Maven plugin Jaxb2 generate java objects correctly.
I did also soap config java file.
but the wsdl generated doesn't have xs:message part so with for example soap ui i cannot make request:
Why method CreateRequest is not generated in wsdl....
Thx
maybe there is a standard for naming the Responses and Requests?

How to specify a list of complexType in XML?

I have XML specified the following:
<xs:element name="getNewsResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="newsItem" type="tns:newsList"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="newsList">
<xs:list itemType="tns:news"/>
</xs:simpleType>
<xs:complexType name="news">
<xs:sequence>
<xs:element name="id" type="xs:string"/>
<xs:element name="date" type="xs:string"/>
<xs:element name="author" type="tns:author"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="shortDescription" type="xs:string"/>
<xs:element name="content" type="xs:string"/>
</xs:sequence>
</xs:complexType>
I would like to have a list of the news in my response. However when I would like to create Java object with jaxb2 the xml returns the folllowing error when I run mvn clean compile -X:
org.xml.sax.SAXParseException: cos-st-restricts.1.1: The type 'newsList' is atomic, so its {base type definition}, 'tns:news', must be an atomic simple type definition or a built-in primitive datatype.
How I should change my XML to be able to compile?
In addition to using the built-in list types, you can create new list types by derivation from existing atomic types. You cannot create list types from existing list types, nor from complex types.
https://www.w3.org/TR/xmlschema-0/#ListDt
This is from one of my working XSD, a user with multiple addresses:
<xs:complexType name="user">
<xs:sequence>
<xs:element name="addresses" type="tns:addressData" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
Note that addressData is a complexType.
I guess this is what you need:
<xs:element name="getNewsResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="newsItems" type="tns:news" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="news">
<xs:sequence>
<xs:element name="id" type="xs:string"/>
<xs:element name="date" type="xs:string"/>
<xs:element name="author" type="tns:author"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="shortDescription" type="xs:string"/>
<xs:element name="content" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://pro.com/balcao/xdto"
xmlns:tns="http://pro.com/balcao/xdto"
elementFormDefault="qualified">
<xs:element name="salvarCliente" >
<xs:complexType>
<xs:sequence>
<xs:element name="nome" type="xs:string" />
<xs:element name="telefone" type="xs:string" />
<xs:element name="provincia" type="xs:string" />
<xs:element name="municipio" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="listarClientes">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="clientes" type="tns:cliente" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="cliente">
<xs:sequence>
<xs:element name="nome" type="xs:string" />
<xs:element name="telefone" type="xs:string" />
<xs:element name="provincia" type="xs:string" />
<xs:element name="municipio" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>

jaxb, xsd substitution complextype

I have the following xsd
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="screen">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="controls" type="Controls"></xs:element>
</xs:sequence>
<xs:attribute name="ref" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:complexType name="Controls">
<xs:sequence>
<xs:element name="control" type="Control" minOccurs="0" maxOccurs="unbounded"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Control" abstract="true">
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="id" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Button">
<xs:complexContent>
<xs:extension base="Control">
<xs:sequence>
<xs:element name="action" type="xs:string"></xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="TextField">
<xs:complexContent>
<xs:extension base="Control">
<xs:sequence>
<xs:element name="value" type="xs:string"></xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
How can I generate the following xml document
<?xml version = "1.0"?>
<screen ref="10_20_25_vwopstellen">
<name>VWOpstellen</name>
<controls>
<button>
<name>btnA</name>
<id>btn_10</id>
<action>click</action>
</button>
<textfield>
<name>fldA</name>
<id>fld_20</id>
</textfield>
</controls>
</screen>
Now it is generating the following xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<screen ref="scrVWOpstellen">
<name>VWOpstellen</name>
<controls>
<control xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Button">
<name>btnA</name>
</control>
<control xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Button">
<name>btnB</name>
</control>
</controls>
</screen>
And when i read my xml document I want a list of contols containing either buttons or textfields
List --> having Button and TextField
I have no idea, i think I should use substitution group but how. I tried something but the xsd is not generating any java code.
thanks
Johan
I'd suggest using an xs:choice, and also defining a target namespace, (http://my.target.namespace in my example), which can make working with JAXB easier in general.
E.g.:
<xs:schema targetNamespace="http://my.target.namespace" xmlns:tns="http://my.target.namespace" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="screen">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="button" type="tns:Button" />
<xs:element name="textfield" type="tns:TextField" />
</xs:choice>
</xs:sequence>
<xs:attribute name="ref" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:complexType name="Control" abstract="true">
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="id" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Button">
<xs:complexContent>
<xs:extension base="tns:Control">
<xs:sequence>
<xs:element name="action" type="xs:string"></xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="TextField">
<xs:complexContent>
<xs:extension base="tns:Control">
<xs:sequence>
<xs:element name="value" type="xs:string"></xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
Another thought, sometimes the type derivation isn't really worth it. For something like this you might also just do:
E.g.:
<xs:schema targetNamespace="http://my.target.namespace" xmlns:tns="http://my.target.namespace" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="screen">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="button" type="tns:Button" />
<xs:element name="textfield" type="tns:TextField" />
</xs:choice>
</xs:sequence>
<xs:attribute name="ref" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:complexType name="Button">
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="id" type="xs:string"></xs:element>
<xs:element name="action" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="TextField">
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="id" type="xs:string"></xs:element>
<xs:element name="value" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>

Java jaxb xsd how to allow maxOccurence

I want following xml output and have following xsd
<msgBody>
<Contato>
<cd_id>11</cd_id>
<property key="name" value="Adde" />
<property key="Phone" value="34343" />
<property key="Address" value="address" />
</Contato>
<Contato>
<cd_id>12</cd_id>
<property key="name" value="BJ" />
<property key="Phone" value="7899" />
<property key="Address" value="sdfkjsdfsdf" />
</Contato>
</msgBody>
I have following xsd where i don't know how can i allow to have multiple property tags
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="msgBody">
<xs:complexType>
<xs:sequence>
<xs:element name="Contato" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:int" name="cd_id"/>
<xs:element type="property_data_type" name="property"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="property_data_type">
<xs:sequence>
<xs:element type="xs:string" name="key"/>
<xs:element type="xs:string" name="value"/>
</xs:sequence>
Try this
<xs:element name="msgBody">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="Contato"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="contao">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:int" name="cd_id"/>
<xs:element maxOccurs="unbounded" ref="propertytype"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="propertytype">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="key"/>
<xs:element type="xs:string" name="value"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Please add maxOccurs.
<xs:element type="property_data_type" name="property" maxOccurs="unbounded"/>
Alternatively, you can put any concrete limit like:
<xs:element type="property_data_type" name="property" maxOccurs="10"/>
It means you can only have a maximum of 10 property elements. The <maxOccurs> indicator specifies the maximum number of times an element can occur.

How to add inline schema in java with xml

I make xml file using Jaxb from a xml schema my existing xml file looks like
<transaction>
<id>
<in>computer</in>
<sn>1234567</sn>
<book>JAVA</book>
<author>klen</author>
</id>
<data>
<dateTime>2011-06-24T17:08:36.3727674+05:30</dateTime>
<key>Err</key>
</data>
</transaction>
but I want to add inline schema before <id> node
My schema is looking like
<xs:schema id="transaction" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="transaction" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="id">
<xs:complexType>
<xs:sequence>
<xs:element name="in" type="xs:string" minOccurs="0" />
<xs:element name="sn" type="xs:string" minOccurs="0" />
<xs:element name="book" type="xs:string" minOccurs="0" />
<xs:element name="author" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="data">
<xs:complexType>
<xs:sequence>
<xs:element name="dateTime" type="xs:dateTime" minOccurs="0" />
<xs:element name="key" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="productData">
<xs:complexType>
<xs:sequence>
<xs:element name="dateTime" type="xs:dateTime" minOccurs="0" />
<xs:element name="key" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
now I want to add this before node under node.
Is it possible using Jaxb in first time?I cannot generate xml file with inline schema using jaxb.Due to the large size of my xml I cannot do this using Dom parser.Presently I try that at first generate xml file with data using jaxb and then rewrite the xml file and add schema under <transaction> node before <id> node.
I will prefer sun provided api.I am new in java so if some codesnips will be added it will be help full for me.

Categories