validating xml data in java - java

My transaction xml is shown below
<?xml version= "1.0"?>
<transactionlist>
<transaction action="c">
<transactionid>t004</transactionid>
<transactiondate>11/06/2013</transactiondate>
<merchantdetails>Sony wholesale Dealer</merchantdetails>
<itempurchased>3</itempurchased>
<amount>40399</amount>
<description>sony laptops</description>
</transaction>
<transaction action="d">
<transactionid>t003</transactionid>
</transaction>
<transaction action="u">
<transactionid>T001</transactionid>
<transactiondate>20/08/2013</transactiondate>
<merchantdetails>samsung Axses</merchantdetails>
<itempurchased>1</itempurchased>
<amount>40000</amount>
<description>samsung smart phone</description>
</transaction>
</transactionlist>
I have parsed the element itempurchased in above xml and stored it in integer variable. How to validate itempurchased only for numbers. that is i want to check whether itempurchased is number. pls provide suggestions

the best way should be validate xml against an xsd, where itempurchased would be of type xsd:int
below the 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="transactionlist">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="transaction">
<xs:complexType>
<xs:sequence>
<xs:element name="transactionid" type="xs:string" />
<xs:element minOccurs="0" name="transactiondate" type="xs:string" />
<xs:element minOccurs="0" name="merchantdetails" type="xs:string" />
<xs:element minOccurs="0" name="itempurchased" type="xs:int" />
<xs:element minOccurs="0" name="amount" type="xs:int" />
<xs:element minOccurs="0" name="description" type="xs:string" />
</xs:sequence>
<xs:attribute name="action" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Here Validating XML against XSD the code for validate xml against xsd

If you are marshaling your xml to a java bean then you may try using the Java6 Bean Validation Framework. Read more about it here:
http://docs.oracle.com/javaee/6/tutorial/doc/gircz.html
It is as simple as putting an annotation on your bean:
public class MyXMLBean {
#Max(10)
#Min(5)
private int itempurchased;
}
The above bean will allow setting the value of itempurchased between min and max values mentioned in the annotations.

Related

src-resolve.4.2: Error resolving component 'xs:name'. It was detected that 'xs:name' is in namespace 'http://www.w3.org/2001/XMLSchema'

I'm creating a Spring based SOAP web service, and I'm trying to return a custom list called AVAILABLE_PACKAGES which contains multiple PACKAGE_ITEM elements. Each one of these PACKAGE_ITEM elements is made by two strings, PACKAGE_NAME and PACKAGE_PRICE.
I tried with multiple configurations, but I keep receiving an error on the line
<xs:element name="availablePackagesList" type="xs:AvailablePackagesList" />
The error reads as follows:
Multiple annotations found at this line:
src-resolve: Cannot resolve the name 'xs:AvailablePackagesList' to a(n) 'type definition' component.
src-resolve.4.2: Error resolving component 'xs:AvailablePackagesList'. It was detected that
'xs:AvailablePackagesList' is in namespace
'http://www.w3.org/2001/XMLSchema', but components from this
namespace are not referenceable from schema document...
This is the code snippet of what I have on my .xsd file
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http:/hello.com/queries"
xmlns:tns="http:/hello.com/queries"
elementFormDefault="qualified">
<!--GetSubscriberAvailablePackages -->
<xs:element
name="MobileGetSubscriberAvailablePackagesRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="network" type="xs:string" />
<xs:element name="username" type="xs:string" />
<xs:element name="password" type="xs:string" />
<xs:element name="subscriberData" type="xs:string" />
<xs:element name="subscriberDataType" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element
name="MobileGetSubscriberAvailablePackagesResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="network" type="xs:string" />
<xs:element name="subscriberData" type="xs:string" />
<xs:element name="subscriberDataType" type="xs:string" />
<xs:element name="response" type="xs:string" />
<xs:element name="responseDetails" type="xs:string" />
<xs:element name="availablePackagesList"
type="xs:AvailablePackagesList" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="AvailablePackagesList">
<xs:sequence>
<xs:element name="AvaillablePackage"
type="xs:AvaillablePackageItem" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="AvaillablePackageItem">
<xs:attribute name="packageName" type="xs:integer"
use="required" />
<xs:attribute name="packagePrice" type="xs:integer"
use="required" />
</xs:complexType>
To eliminate your immediate error and one subsequent error,
change type="xs:AvailablePackagesList" to type="tns:AvailablePackagesList", and
changetype="xs:AvaillablePackageItem" to type="tns:AvaillablePackageItem"
because both are in the http:/yobitelecom.com/queries namespace, not the http://www.w3.org/2001/XMLSchema namespace.

Prefix not appeared n on generated XML

I've this XSD Schema definition
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:ns2="http://www.xxxx.xxxxxxxxx.xxxxxxxx/xxxx/xxxx">
<xs:element name="myRootElemnt">
<xs:complexType>
<xs:sequence>
<xs:element name="messageType">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:short" name="id" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element type="xs:int" name="time" />
<xs:element type="xs:short" name="date" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The generated XML is:
But, the XML still incomplete, it must contains xmlns:ns2="http://www.xxxx.xxxxxxxxx.xxxxxxxx/xxxx/xxxx"> and the prefix ns2 for the root element , like this:
Any Solutions?

s4s-att-invalid-value: Invalid attribute value for 'name' in element 'element'

XML
<ns2:Response xmlns:ns2="http://test.com/" Id="122212">
<Infos size="1">
<Info>
<name>test</name>
</Info>
</Infos>
</ns2:Response>
Generated XSD
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<!-- XML Schema Generated from XML Document on Mon Feb 20 2017 23:20:03 GMT+0530 (India Standard Time) -->
<!-- with XmlGrid.net Free Online Service http://xmlgrid.net -->
<xs:element name="ns2:Response">
<xs:complexType>
<xs:sequence>
<xs:element name="Infos">
<xs:complexType>
<xs:sequence>
<xs:element name="Info">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="size" type="xs:int"></xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="xmlns:ns2" type="xs:string"></xs:attribute>
<xs:attribute name="Id" type="xs:int"></xs:attribute>
</xs:complexType>
</xs:element>
Error
SAX Exception: s4s-att-invalid-value: Invalid attribute value for
'name' in element 'element'. Recorded reason:
cvc-datatype-valid.1.2.1: 'ns2:Response' is not a valid value for
'NCName'.
There are several changes required for your XML and XSD, including:
Change <xs:element name="ns2:Response"> to <xs:element
name="Response"> because element name declarations must be
non-colonized names (NCNAMEs).
Delete <xs:attribute name="xmlns:ns2".../> because namespaces
cannot be declared as attributes.
Add a targetNamespace to the XSD that matches the namespace of the root element in the XML document.
Import a separate XSD for those elements that you wish to be in no namespace (given that your root elements is in a namespace). You must use a separate XSD to accomplish this.
Altogether, your XML,
<?xml version="1.0" encoding="UTF-8"?>
<ns2:Response xmlns:ns2="http://test.com/"
Id="122212">
<Infos size="1">
<Info>
<name>test</name>
</Info>
</Infos>
</ns2:Response>
will validate successfully against these XSD:
Main
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
xmlns:ns="http://test.com/"
targetNamespace="http://test.com/">
<xs:import schemaLocation="Infos.xsd"/>
<xs:element name="Response">
<xs:complexType>
<xs:sequence>
<xs:element ref="Infos"/>
</xs:sequence>
<xs:attribute name="Id" type="xs:int"></xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>
Imported (Infos.xsd)
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Infos">
<xs:complexType>
<xs:sequence>
<xs:element name="Info">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="size" type="xs:int"></xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>
I was able to get a parsed file by, as the above answer suggests, adding a target namepsace to the schema and by adding the ns prefix to all elements.
<?xml version="1.0" encoding="UTF-8"?>
<ns2:Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://test.com testq1.xsd" xmlns:ns2="http://test.com" Id="122212">
<ns2:Infos size="1">
<ns2:Info>
<ns2:name>test</ns2:name>
</ns2:Info>
</ns2:Infos>
and the schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://test.com"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="Response">
<xs:complexType>
<xs:sequence>
<xs:element name="Infos">
<xs:complexType>
<xs:sequence>
<xs:element name="Info">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="size" type="xs:int"></xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="Id" type="xs:int"></xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>
You can approach this in 2 ways.
Approach 1 - multiple schemas
The more typical approach is to have a schema one for each namespace used.
So you end up with schemas that look like this
SampleXml0.xsd
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Studio 2017 (https://www.liquid-technologies.com) -->
<xs:schema xmlns:ns2="http://test.com/" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://test.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import schemaLocation="C:\Temp\StackOverflow\42351409\SampleXml1.xsd" />
<xs:element name="Response">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" ref="Infos" />
</xs:sequence>
<xs:attribute name="Id" type="xs:unsignedInt" use="optional" />
</xs:complexType>
</xs:element>
</xs:schema>
SampleXml1.xsd
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Studio 2017 (https://www.liquid-technologies.com) -->
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Infos">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="Info">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="name" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="size" type="xs:unsignedByte" use="optional" />
</xs:complexType>
</xs:element>
</xs:schema>
Approach 2 - changing elementFormDefault
Now this is quite specific to your example, but as its only the root element that is qualified with a namespace its possible to change elementFormDefault to unqualified. This has the effect of forcing the elements defined as root element in the schema to have a namespace qualification, while the other elements do not.
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio 2017 (https://www.liquid-technologies.com)-->
<xs:schema xmlns:ns2="http://test.com/" attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://test.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Response">
<xs:complexType>
<xs:sequence>
<xs:element name="Infos" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="Info" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="size" type="xs:unsignedByte" use="optional" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="Id" type="xs:unsignedInt" use="optional" />
</xs:complexType>
</xs:element>
</xs:schema>
I would probably recommend creating multiple schemas as elementformdefault is typically overlooked by client implementations and ignored.
Why three <xs:sequence> elements? do you really need that? Could you explain better how your code is organized? Usually it is pretty simple, like this:
<xsd:element name="ns2:Response">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="name" type="xs:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>

How to handle array in xml / xsd for generating a POJO class?

Sample XML response from REST WS -
<UserInfoDataContract xmlns="http://schemas.datacontract.org/2004/07/Interzoic.SSO.Shared" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<DisplayName>Test User</DisplayName>
<Email>test#test.com</Email>
<FirstName>Test</FirstName>
<IsSuperUser>false</IsSuperUser>
<LastName>User</LastName>
<Password>testuser1</Password>
<PortalID>0</PortalID>
<Roles xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:string>Registered Users</a:string>
</Roles>
<UserID>43</UserID>
<Username>testuser</Username>
</UserInfoDataContract>
XSD generated using http://xmlgrid.net/xml2xsd.html
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<!-- XML Schema Generated from XML Document on Thu Apr 09 2015 11:18:33 GMT-0500 (CDT) -->
<!-- with XmlGrid.net Free Online Service http://xmlgrid.net -->
<xs:element name="UserInfoDataContract">
<xs:complexType>
<xs:sequence>
<xs:element name="DisplayName" type="xs:string"></xs:element>
<xs:element name="Email" type="xs:string"></xs:element>
<xs:element name="FirstName" type="xs:string"></xs:element>
<xs:element name="IsSuperUser" type="xs:string"></xs:element>
<xs:element name="LastName" type="xs:string"></xs:element>
<xs:element name="Password" type="xs:string"></xs:element>
<xs:element name="PortalID" type="xs:int"></xs:element>
<xs:element name="Roles">
<xs:complexType>
<xs:sequence>
<xs:element name="a:string" type="xs:string"></xs:element>
</xs:sequence>
<xs:attribute name="xmlns:a" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="UserID" type="xs:int"></xs:element>
<xs:element name="Username" type="xs:string"></xs:element>
</xs:sequence>
<xs:attribute name="xmlns" type="xs:string"></xs:attribute>
<xs:attribute name="xmlns:i" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
When i try to create JAXB Classes from the above XSD in eclipse, it gives me errors related to
<xs:attribute name="xmlns" type="xs:string"></xs:attribute>
<xs:attribute name="xmlns:i" type="xs:string"></xs:attribute>
and
<xs:attribute name="xmlns:a" in Roles element
and
<xs:element name="a:string" type="xs:string"></xs:element>
So i removed them and added
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.datacontract.org/2004/07/Interzoic.SSO.Shared" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays" targetNamespace="http://www.w3.org/2001/XMLSchema-instance" elementFormDefault="qualified" attributeFormDefault="unqualified">
on the top.
How will the "Roles" from my XML be referenced in the XSD so that i can create a correct POJO class?
Taking reference from http://www.w3.org/TR/xmlschema-0/#ListDt, lists should be declared this way
<xsd:simpleType name="listOfMyIntType">
<xsd:list itemType="myInteger"/>
</xsd:simpleType>
I am not able to figure out how i can apply this to my XSD. Any help will be appreciated.
I think in xml in
<Roles xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
the xmlns:a
is considered as attribute by the ide that generates xsd, that's why in generated schema you have
<xs:attribute name="xmlns" type="xs:string"></xs:attribute>
So I agree with deleting
<xs:attribute name="xmlns:a" type="xs:string"></xs:attribute>
<xs:attribute name="xmlns" type="xs:string"></xs:attribute>
<xs:attribute name="xmlns:i" type="xs:string"></xs:attribute>
from xsd.
My only guess is try setting
targetNamespace="http://schemas.datacontract.org/2004/07/Interzoic.SSO.Shared"
as it is used explicitly in your xml

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