Unmarshalling nested objects using JAXB - java

I am using JAXB and can't figure out why my nested objects aren't being unmarshalled. I am generating the classes via the XJC command.
For example, when I unmarshall the Works object, the Composers collection always contains one Composer instance will a NULL name.
My XML looks like this:
<Works>
<Work>
<Composer>
<Name>Test Name</Name>
</Composer>
</Work>
</Works>
and XSD is like this:
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
jxb:version="2.0" xmlns:tns="http://www.example.org/test/"
targetNamespace="http://www.example.org/test/">
<element name="Works" type="tns:Work"></element>
<complexType name="Work">
<sequence>
<element name="Composers" type="tns:Composer" maxOccurs="unbounded"
minOccurs="1">
</element>
</sequence>
</complexType>
<complexType name="Composer">
<sequence>
<element name="Name" type="string">
</element>
</sequence>
</complexType>
And my code that does the unmarshalling:
JAXBContext jc = JAXBContext.newInstance("mypackagename");
Unmarshaller um = jc.createUnmarshaller();
Works works = (Works)um.unmarshal(new FileReader("src/main/resources/works.xml"));
Work work = works.getWorks().get(0);
Composer composer = work.getComposers().get(0);
System.out.println(composer.getName());
Name is always NULL, even though I know it has a value.

You could have an XML schema like:
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
jxb:version="2.0" xmlns:tns="http://www.example.org/test/"
targetNamespace="http://www.example.org/test/">
<element name="Works" type="tns:Works"></element>
<complexType name="Works">
<sequence>
<element name="Work" type="tns:Work" maxOccurs="unbounded"/>
</sequence>
</complexType>
<complexType name="Work">
<sequence>
<element name="Composer" type="tns:Composer" maxOccurs="unbounded"/>
</sequence>
</complexType>
<complexType name="Composer">
<sequence>
<element name="Name" type="string"/>
</sequence>
</complexType>
</schema>
That corresponds to the following XML:
<Works xmlns="http://www.example.org/test/">
<Work>
<Composer>
<Name>Test Name</Name>
</Composer>
</Work>
</Works>

Related

WSDL Array Complex Type not typed in Java output

When using the Axis2 WSDL2Java tool, the array seems to be not typed and therefore have to cast an object to add it.
<element name="FunnyObject" nillable="true">
<complexType>
<sequence>
<element name="id" type="string" />
<element name="joke" type="string" />
</sequence>
</complexType>
</element>
<element name="FunnyObjects" nillable="true">
<complexType>
<complexContent mixed="false">
<restriction base="SOAP-ENC:Array">
<attribute ref="SOAP-ENC:arrayType" wsdl:arrayType="FunnyObject[]" />
</restriction>
</complexContent>
</complexType>
</element>
Any ideas what edits I need to do? New to WSDL as just learning it for a course (unfortunately in 2019).

SAAJ0511: Unable to create envelope from given source

I'm new to spring boot and web services and am trying to create a soap webservice, I followed a tutorial but i keep getting the below error:
SAAJ0511: Unable to create envelope from given source
the code snippet is as below:
thats the xsd :
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.binarystorage.com/binary-storage-soap"
xmlns:tns="http://www.binarystorage.com/binary-storage-soap"
elementFormDefault="qualified">
<element name="store-request" type="tns:binaryData"/>
<element name="store-response" type="tns:idBody"/>
<element name="retrieve-request" type="tns:idBody"/>
<element name="retrieve-response" type="tns:binaryData"/>
<element name="remove-request" type="tns:idBody"/>
<element name="remove-response"/>
<complexType name="binaryData">
<sequence>
<element name="name" type="string"/>
<element name="content" type="string"/>
<element name="content-type" type="string"/>
<element name="meta" minOccurs="0" maxOccurs="unbounded">
<complexType>
<attribute name="key" type="string"></attribute>
<attribute name="value" type="string"></attribute>
</complexType>
</element>
</sequence>
</complexType>
<complexType name="idBody">
<sequence>
<element name="id" type="string"/>
</sequence>
</complexType>
</schema>
and the screen shot below is my try for an endpoint :
Please advice
Thanks

recursive xsd generates java class with Objects instead of the given Type

So, I have an object which can contain a list of Objects of that type. I wrote an XSD which looks something like this:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="myNamespace" elementFormDefault="qualified">
<complexType name="BinModel">
<sequence>
<element type="string" name="min" />
<element type="string" name="max" />
<element type="string" name="fieldname" />
<element type="int" name="defaultValue" />
<element xmlns:ref="BinModel" name="innerBins" maxOccurs="unbounded" minOccurs="0" />
</sequence>
</complexType>
<element name="AllBins">
<complexType>
<sequence>
<element type="string" name="fieldnames" maxOccurs="unbounded" minOccurs="0"/>
<element type="int" name="defaultValue"/>
<element xmlns:type="BinModel" name="outerBins" maxOccurs="unbounded" minOccurs="0" />
</sequence>
</complexType>
</element>
</schema>
It produces two java classes, BinModel and AllBins respectively, but in each of those classes, even though I specify that they contain a list of type BinModel, it produces a List of type Object.
How do I generate a class which has a List of BinModels?
So I realized something was wrong when it took me adding xmlns before type and ref in order to not show errors in the editor. I looked at another xsd for a recursively defined Object somewhere else in my stupidly huge codebase to try and find a solution and discovered two things.
1. We were defining our own namespace
2. We referenced our own objects within that namespace.
So the corrected code looks something like this:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:myProject="projectNamespace" targetNamespace="myNamespace" elementFormDefault="qualified">
<complexType name="BinModel">
<sequence>
<element type="string" name="min" />
<element type="string" name="max" />
<element type="string" name="fieldname" />
<element type="int" name="defaultValue" />
<element type="myProject:BinModel" name="innerBins" maxOccurs="unbounded" minOccurs="0" />
</sequence>
</complexType>
<element name="AllBins">
<complexType>
<sequence>
<element type="string" name="fieldnames" maxOccurs="unbounded" minOccurs="0"/>
<element type="int" name="defaultValue"/>
<element type="myProject:BinModel" name="outerBins" maxOccurs="unbounded" minOccurs="0" />
</sequence>
</complexType>
</element>
</schema>

Xsd conditional implementation

I am trying to validate a pdf files documentation using xsd, where I convert the given pdf to xml and parse it through the schema xsd, and it validates, but lets assume there is a heading and it has 2 subheadings how do I change to xsd schema such that for a particular type of heading it should and must have minimum 2 subheadings of particular text(words/sentences), how do I add conditions to the xsd file for it validate specifically designed documents ?
here is the xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="elements">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="element"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="element">
<xs:complexType>
<xs:sequence>
<xs:element ref="pageno"/>
</xs:sequence>
<xs:attribute name="level" use="required" type="xs:integer"/>
<xs:attribute name="title" use="required"/>
<xs:attribute name="type" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="pageno" type="xs:integer"/>
</xs:schema>
and here is the xml I used to generate this xsd:
<elements>
<element type ="Introduction" level="1" title="Introduction">
<pageno>4</pageno>
</element>
<element type ="Introduction" level="2" title="Enhancements to the HP CSA vCenter Simple Compute">
<pageno>4</pageno>
</element>
<element type ="System requirements" level="1" title="System requirements">
<pageno>5</pageno>
</element>
<element type ="System requirements" level="2" title="Software components">
<pageno>5</pageno>
</element>
<element type ="Configuration requirements" level="1" title="Configuration requirements">
<pageno>7</pageno>
</element>
<element type ="Configuration requirements" level="2" title="Installing content capsule">
<pageno>7</pageno>
</element>
<element type ="Configuring offerings in HP CSA" level="1" title="Configuring offerings in HP CSA">
<pageno>8</pageno>
</element>
<element type ="Configuring offerings in HP CSA" level="2" title="Configuring subscriber options">
<pageno>8</pageno>
</element>
<element type ="Configuring subscriber options" level="2" title="Adding providers">
<pageno>8</pageno>
</element>
<element type ="Adding providers" level="2" title="Associating resource offerings with providers">
<pageno>9</pageno>
</element>
<element type ="Associating resource offerings with providers" level="2" title="Changing component properties">
<pageno>10</pageno>
</element>
<element type ="Changing component properties" level="2" title="Creating the service offering">
<pageno>12</pageno>
</element>
<element type ="Creating the service offering" level="2" title="Publishing the service offering">
<pageno>13</pageno>
</element>
<element type ="Publishing the service offering" level="3" title="Publishing service offering to a Catalog">
<pageno>13</pageno>
</element>
<element type ="Subscribing to the service" level="1" title="Subscribing to the service">
<pageno>14</pageno>
</element>
<element type ="Subscribing to the service" level="2" title="Canceling a subscription">
<pageno>14</pageno>
</element>
<!-- <element type ="adasdasd" level = "5" title= "dasdsad">
</element> -->
<element type ="Limitations" level="1" title="Limitations">
<pageno>16</pageno>
</element>
<element type ="Appendix A: HP Operations Orchestration flows" level="1" title="Appendix A: HP Operations Orchestration flows">
<pageno>17</pageno>
</element>
<element type ="Appendix B: Integrating with IP Address Management solutions" level="1" title="Appendix B: Integrating with IP Address Management solutions">
<pageno>19</pageno>
</element>
<element type ="Additional resources" level="1" title="Additional resources">
<pageno>20</pageno>
</element>
<element type ="Send Documentation Feedback" level="1" title="Send Documentation Feedback">
<pageno>21</pageno>
</element>
</elements>
If you think I am lacking in clarity in question then please let me know I will answer any queries.
Thank you
You can define a Schema type of your own that is a heading that must contain specific text, and use the conditional type assignment feature of XSD 1.1, if your implementation supports it.
A more widely supported approach is to embed Schematron rules in your schema to do what you want.
Remember that if you over-constrain your input the schema may become fragile -- that is, hard to maintain in the event of changes.

xml schema validation error “The content of element 'flowPara' is not complete”

I need to valid XSD for a XML file.
This is xml file.
<?xml version="1.0" encoding="UTF-8"?>
<edge xmlns="http://www.example.org/flow">
<flowPara
style="letter-spacing:0px;fill:#000000;
font-size:9pt;font-family:Arial;text-anchor:start;text-align:start;"
id="textArea_38">
Text Flow checking and
<flowSpan style="fill:#FA0101; ">
font color change
text
<flowSpan style="fill:#C5A2A2; " />
</flowSpan>
in
<flowSpan
style="font-style:italic;letter-spacing:0px;fill:#000000;
font-size:9pt;font-family:Arial;text-anchor:start;text-align:start;">text
area
</flowSpan>
.
<flowSpan style="letter-spacing:0px;">
<flowSpan
style="text-decoration:underline;letter-spacing:0px;fill:#000000;
font-size:9pt;font-family:Arial;text-anchor:start;text-align:start;">Text
Flow
</flowSpan>
checking and
<flowSpan style="fill:#FA0101; ">
font color
change text
<flowSpan style="fill:#C5A2A2; ">
<flowSpan style="fill:#000000;
">in text area.</flowSpan>
</flowSpan>
</flowSpan>
</flowSpan>
</flowPara>
</edge>
This is XSD file which i created .
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/flow"
xmlns:tns="http://www.example.org/flow" elementFormDefault="qualified">
<element name="edge">
<complexType>
<sequence>
<element name="flowPara" maxOccurs="unbounded">
<complexType mixed="true">
<sequence>
<element name="flowspan" maxOccurs="unbounded">
<complexType mixed="true">
<sequence>
<element name="flowspan" maxOccurs="unbounded">
<complexType mixed="true">
<simpleContent>
<extension base="string">
<attribute name="style" type="string" />
</extension>
</simpleContent>
</complexType>
</element>
</sequence>
<attribute name="style" type="string" />
</complexType>
</element>
</sequence>
<attribute name="style" type="string" />
<attribute name="id" type="string" />
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>
i faced this kind of exception
Exception: cvc-complex-type.2.4.b: The content of element 'flowPara' is not complete. One of '{"http://www.example.org/flow":flowspan}' is expected.
1) In your schema, you have flowspan with the s in lowercase in several places. Change that to flowSpan.
2) You declared mixed content, but your flowSpan element is not optional, so it will always be required and will not validate it if the contents of flowSpan don't contain another flowSpan. Add minOccurs="0" so it becomes optional.
3) You don't need to declare simple content if you have mixed content with an optional nested flowSpan. Your schema could be reorganized using a reference for flowSpan, since it's used recursively. You could try this:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/flow"
xmlns:tns="http://www.example.org/flow"
elementFormDefault="qualified">
<element name="edge">
<complexType>
<sequence>
<element name="flowPara" maxOccurs="unbounded">
<complexType mixed="true">
<sequence>
<element ref="tns:flowSpan" maxOccurs="unbounded"/>
</sequence>
<attribute name="style" type="string" />
<attribute name="id" type="string" />
</complexType>
</element>
</sequence>
</complexType>
</element>
<element name="flowSpan">
<complexType mixed="true">
<sequence>
<element ref="tns:flowSpan" minOccurs="0" maxOccurs="unbounded"/>
</sequence>
<attribute name="style" type="string" />
</complexType>
</element>
</schema>
You didn't say minOccurs='0', so it requires one.

Categories