NullPointerException when unmarshalling with JAXB 1.0

I get the following error when unmarshalling with JAXB 1.0:

Exception java.lang.NullPointerException - Profile2User(): null java.lang.NullPointerException

java.lang.NullPointerException

at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:139)

at com.sun.xml.bind.unmarshaller.DOMScanner.parse(DOMScanner.java:72)

at com.kpn.alk.dispatcher.jaxb.impl.runtime.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:168)

at com.kpn.alk.business.Profile2User.parse(Profile2User.java:139)

at com.kpn.alk.action.voip.admin.AdminAction.initAction(AdminAction.java:289)

My code in the Profile2User class looks like this:

package com.kpn.alk.business;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.w3c.dom.NamedNodeMap;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import javax.xml.bind.JAXBContext;

import javax.xml.bind.JAXBException;

import javax.xml.bind.Unmarshaller;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.DocumentBuilder;

import com.kpn.alk.dispatcher.jaxb.Profile;

import com.kpn.alk.action.voip.VoipActionSupport;

public class Profile2User extends VoipActionSupport{

private static final ....;

....;

private NodeList profile_node = null;

public Profile2User(NodeList list) {

profile_node = list;

}

public void parse() {

Node root = null;

try {

JAXBContext jc = JAXBContext.newInstance("com.kpn.alk.dispatcher.jaxb", this.getClass().getClassLoader());

Unmarshaller unmarshaller = jc.createUnmarshaller();

unmarshaller.setValidating(true);

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

dbf.setNamespaceAware(true);

DocumentBuilder db = dbf.newDocumentBuilder();

Document document = db.newDocument();

if(profile_node!=null) {

int size = profile_node.getLength();

for (int i = 0; i < size; i++) {

Node node = profile_node.item(i);

Element e = (Element)node;

System.out.println("DOM Implementation Element: " + e.getClass().getName());

if(node!=null){

Node child = node.getFirstChild();

String name = child.getNodeName();

if (name.equals(PROFILE_ELEMENT)) {

root = document.importNode(child, true);

}

}

}

} else {

}

//StringBuffer xmlStr = new StringBuffer( "<?xml version=\"1.0\"?><profile><mt></mt><vt><ckr>DKVF1513</ckr></vt></profile>" );

//Profile profile = (Profile)unmarshaller.unmarshal( new StreamSource( new StringReader( xmlStr.toString() ) ) );

Profile profile = (Profile)unmarshaller.unmarshal(root.getOwnerDocument() );

} catch (JAXBException e){

System.out.println("code = "+e.getErrorCode()+" message = "+e.toString() ) ;

} catch (Exception e ) {

System.out.println("Exception "+e.getClass().getName()+" - Profile2User(): " + e.getMessage()+" "+ e.toString());

e.printStackTrace();

}

}

My xsd is as follows:

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:isp="http://dualplay.kpn.com/webservices/isp/DualPlay_CC_Manager_UserRead">

<xs:element name="profile">

<xs:annotation>

<xs:documentation>Profile View: KPN.COM user authorization profile: fixed and mobile</xs:documentation>

</xs:annotation>

<xs:complexType>

<xs:sequence>

<xs:element name="errors" minOccurs="0">

<xs:complexType>

<xs:sequence>

<xs:element name="error" type="error" maxOccurs="unbounded"/>

</xs:sequence>

</xs:complexType>

</xs:element>

<xs:element name="vt" type="vtType" minOccurs="0">

<xs:annotation>

<xs:documentation>VT View</xs:documentation>

</xs:annotation>

</xs:element>

<xs:element name="mt" type="mtType" minOccurs="0">

<xs:annotation>

<xs:documentation>MT View</xs:documentation>

</xs:annotation>

</xs:element>

<xs:element name="subaut" type="subautType" minOccurs="0">

<xs:annotation>

<xs:documentation>Subaut View: IAM subauthorizations</xs:documentation>

</xs:annotation>

</xs:element>

<xs:element name="personalized" type="personalizedType" minOccurs="0">

<xs:annotation>

<xs:documentation>Personalizations</xs:documentation>

</xs:annotation>

</xs:element>

</xs:sequence>

</xs:complexType>

</xs:element>

<xs:complexType name="error">

<xs:annotation>

<xs:documentation>Error during authorizations retrieval</xs:documentation>

</xs:annotation>

<xs:sequence>

<xs:element name="dynamictext" type="xs:string"/>

<xs:element name="errorcode" type="xs:unsignedShort"/>

<xs:element name="message" type="xs:string"/>

</xs:sequence>

</xs:complexType>

<xs:complexType name="mtType">

<xs:annotation>

<xs:documentation>Type of mobile authorisation</xs:documentation>

</xs:annotation>

<xs:sequence>

<xs:element name="roles" type="rolesType"/>

<xs:element name="loyalty" type="loyaltyType" minOccurs="0"/>

</xs:sequence>

</xs:complexType>

<xs:complexType name="loyaltyType">

<xs:annotation>

<xs:documentation>Loyalty type</xs:documentation>

</xs:annotation>

<xs:sequence>

<xs:element name="customer-id" type="xs:string"/>

</xs:sequence>

</xs:complexType>

<xs:complexType name="vtType">

<xs:annotation>

<xs:documentation>Type of fixed authorisation</xs:documentation>

</xs:annotation>

<xs:sequence>

<xs:element name="ckr" type="xs:string" minOccurs="0"/>

<xs:element name="ckrtemp" type="xs:string" minOccurs="0"/>

<xs:element name="roles" type="rolesType"/>

<xs:element name="locations" type="locationsType">

<xs:annotation>

<xs:documentation>DualPlay View</xs:documentation>

</xs:annotation>

</xs:element>

</xs:sequence>

</xs:complexType>

<xs:complexType name="rolesType">

<xs:annotation>

<xs:documentation>List of roles</xs:documentation>

</xs:annotation>

<xs:sequence>

<xs:element name="role" type="xs:unsignedShort" maxOccurs="unbounded"/>

</xs:sequence>

</xs:complexType>

<xs:complexType name="locationsType">

<xs:annotation>

<xs:documentation>Locations for installed base</xs:documentation>

</xs:annotation>

<xs:sequence maxOccurs="unbounded">

<xs:element name="location" type="locationType" minOccurs="0"/>

</xs:sequence>

<xs:attribute name="selecteddualplay" type="xs:string"/>

</xs:complexType>

<xs:complexType name="locationType">

<xs:annotation>

<xs:documentation>Location for installed base</xs:documentation>

</xs:annotation>

<xs:sequence>

<xs:element name="streetname" type="xs:string"/>

<xs:element name="housenumber" type="xs:unsignedShort"/>

<xs:element name="housenumberext" type="xs:string"/>

<xs:element name="postalcode" type="xs:string"/>

<xs:element name="cityname" type="xs:string"/>

<xs:element name="dualplays" type="dualPlaysType"/>

</xs:sequence>

</xs:complexType>

<xs:complexType name="dualPlaysType">

<xs:annotation>

<xs:documentation>Dual play products</xs:documentation>

</xs:annotation>

<xs:sequence>

<xs:element name="dualplay" type="dualPlayType" maxOccurs="unbounded"/>

</xs:sequence>

</xs:complexType>

<xs:complexType name="dualPlayType">

<xs:annotation>

<xs:documentation>Dual play product</xs:documentation>

</xs:annotation>

<xs:sequence>

<xs:element name="dualplayid" type="xs:string"/>

<xs:element name="serviceid" type="xs:string"/>

<xs:element name="dualplaytype" type="abboType"/>

<xs:element name="phase" type="phaseType"/>

<xs:element name="initialized" type="xs:boolean"/>

<xs:element name="ispid" type="xs:string"/>

<xs:element name="subscriberid" type="xs:string"/>

<xs:element name="primarymailboxadress" type="xs:string"/>

<xs:element name="voipaccounts" type="voipaccountsType"/>

<xs:choice>

<xs:choice>

<xs:element name="DualPlay_CC_Manager_UserReadResult" type="UserReadType" minOccurs="0"/>

<xs:element name="fasttrackerror" minOccurs="0"/>

<xs:element name="planetinternethetnet" minOccurs="0"/>

</xs:choice>

</xs:choice>

<xs:element name="authorizations" type="authorizationsType"/>

</xs:sequence>

</xs:complexType>

<xs:complexType name="UserReadType">

<xs:sequence>

<xs:element name="CallReferenceID" type="xs:string"/>

<xs:element name="ResultCode" type="xs:string"/>

<xs:element name="ResultDescription" type="xs:string"/>

<xs:element name="AccountID" type="xs:string"/>

<xs:element name="BankAccount" type="xs:string"/>

<xs:element name="BankAccountName" type="xs:string"/>

<xs:element name="BankAccountCity" type="xs:string"/>

<xs:element name="PrimaryMailboxID" type="xs:string"/>

<xs:element name="FreeMailboxThreshold" type="xs:string"/>

<xs:element name="MailboxCount" type="xs:string"/>

<xs:element name="Mailboxes">

<xs:complexType>

<xs:sequence>

<xs:element name="Mailbox" maxOccurs="unbounded">

<xs:complexType>

<xs:sequence>

<xs:element name="MailboxID" type="xs:string"/>

<xs:element name="AccountName" type="xs:string"/>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:sequence>

</xs:complexType>

<xs:simpleType name="abboType">

<xs:restriction base="xs:string">

<xs:enumeration value="go"/>

<xs:enumeration value="lite"/>

<xs:enumeration value="basic"/>

<xs:enumeration value="extra"/>

</xs:restriction>

</xs:simpleType>

<xs:simpleType name="phaseType">

<xs:restriction base="xs:string">

<xs:enumeration value="aangelegd"/>

<xs:enumeration value="in opheffing"/>

</xs:restriction>

</xs:simpleType>

<xs:complexType name="voipaccountsType">

<xs:sequence>

<xs:element name="voipaccount" type="voipaccountType" maxOccurs="unbounded"/>

</xs:sequence>

</xs:complexType>

<xs:complexType name="voipaccountType">

<xs:sequence>

<xs:element name="telnumber" type="xs:string"/>

<xs:element name="callbundle" type="callBundleType"/>

</xs:sequence>

<xs:attribute name="type" type="accountType"/>

</xs:complexType>

<xs:simpleType name="callBundleType">

<xs:restriction base="xs:string">

<xs:enumeration value="night"/>

<xs:enumeration value="evening"/>

<xs:enumeration value="always"/>

</xs:restriction>

</xs:simpleType>

<xs:simpleType name="accountType">

<xs:restriction base="xs:string">

<xs:enumeration value="main"/>

<xs:enumeration value="extra"/>

<xs:enumeration value="nomadic"/>

</xs:restriction>

</xs:simpleType>

<xs:complexType name="subautType">

<xs:annotation>

<xs:documentation>Type for subauthorizations from Anox IAM</xs:documentation>

</xs:annotation>

<xs:sequence>

<xs:element name="service" type="xs:string" maxOccurs="unbounded"/>

<xs:choice minOccurs="0">

<xs:element name="phone" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>

<xs:element name="mail" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>

</xs:choice>

</xs:sequence>

<xs:attribute name="id" type="xs:string" use="required"/>

</xs:complexType>

<xs:complexType name="personalizedType">

<xs:annotation>

<xs:documentation>Type for personalizations</xs:documentation>

</xs:annotation>

<xs:sequence>

<xs:element name="row" type="rowType" minOccurs="0" maxOccurs="unbounded"/>

<xs:element name="resultset" type="xs:string"/>

</xs:sequence>

</xs:complexType>

<xs:complexType name="rowType">

<xs:annotation>

<xs:documentation>Type for personalizations rows</xs:documentation>

</xs:annotation>

<xs:sequence>

<xs:element name="column" type="columnType" maxOccurs="unbounded"/>

</xs:sequence>

<xs:attribute name="rownum" type="xs:unsignedShort"/>

</xs:complexType>

<xs:complexType name="columnType">

<xs:annotation>

<xs:documentation>Type for personalizations columns</xs:documentation>

</xs:annotation>

<xs:simpleContent>

<xs:extension base="xs:string">

<xs:attribute name="name" type="xs:string"/>

<xs:attribute name="type" type="xs:string"/>

</xs:extension>

</xs:simpleContent>

</xs:complexType>

<xs:complexType name="authorizationsType">

<xs:annotation>

<xs:documentation>Type for authorizations, i.e. services the user is

authorized for</xs:documentation>

</xs:annotation>

<xs:sequence>

<xs:element name="service" type="xs:integer" minOccurs="0"

maxOccurs="unbounded"/>

</xs:sequence>

</xs:complexType>

</xs:schema>

And my xml is as follows:

<?xml version="1.0"?>

<profile>

<vt>

<roles>

<role>29</role>

<role>18</role>

<role>2</role>

<role>20</role>

<role>10</role>

<role>24</role>

<role>9</role>

<role>15</role>

<role>11</role>

<role>4</role>

<role>19</role>

<role>8</role>

<role>12</role>

</roles>

<ckr>CQCK6934</ckr>

<locations selecteddualplay="5000000001">

<location>

<streetname>De Horst</streetname>

<housenumber>25</housenumber>

<housenumberext>b</housenumberext>

<postalcode>9781 XW</postalcode>

<cityname>BEDUM</cityname>

<dualplays>

<dualplay>

<dualplayid>5000000001</dualplayid>

<serviceid>dienstnr6339</serviceid>

<initialized>true</initialized>

<abbotype>lite</abbotype>

<ispid>80000049</ispid>

<subscriberid>5108</subscriberid>

<voipaccounts>

<voipaccount type="main">

<telnumber>0503013460</telnumber>

<callbundle>evening</callbundle>

</voipaccount>

<voipaccount type="extra">

<telnumber>0504889663</telnumber>

<callbundle>always</callbundle>

</voipaccount>

<voipaccount type="nomadic">

<telnumber>0843553431</telnumber>

<callbundle>evening</callbundle>

</voipaccount>

<voipaccount type="nomadic">

<telnumber>0843553645</telnumber>

<callbundle>evening</callbundle>

</voipaccount>

</voipaccounts>

<DualPlay_CC_Manager_UserReadResponse>

<DualPlay_CC_Manager_UserReadResult>

<CallReferenceID>

M2Y4NWM3OjEwNmM0YTk1NTJjOi03ZjE3</CallReferenceID>

<ResultCode>2.0.0</ResultCode>

<ResultDescription>Success.</ResultDescription>

<AccountID>80001409</AccountID>

<BankAccount>1234567</BankAccount>

<BankAccountName>P. Paulussen</BankAccountName>

<BankAccountCity/>

<PrimaryMailboxID>260261</PrimaryMailboxID>

<FreeMailboxThreshold>1</FreeMailboxThreshold>

<MailboxCount>3</MailboxCount>

<Mailboxes>

<Mailbox>

<MailboxID>260261</MailboxID>

<AccountName>ipbtest150</AccountName>

</Mailbox>

<Mailbox>

<MailboxID>258325</MailboxID>

<AccountName>ipbtest150A</AccountName>

</Mailbox>

<Mailbox>

<MailboxID>259221</MailboxID>

<AccountName>ipbtest150B</AccountName>

</Mailbox>

</Mailboxes>

</DualPlay_CC_Manager_UserReadResult>

</DualPlay_CC_Manager_UserReadResponse>

</dualplay>

</dualplays>

</location>

<location>

<streetname>De Dorst</streetname>

<housenumber>26</housenumber>

<housenumberext/>

<postalcode>9781 XW</postalcode>

<cityname>BEDUM</cityname>

<dualplays>

<dualplay>

<dualplayid>5000000002</dualplayid>

<serviceid>dienstnr6339</serviceid>

<initialized>true</initialized>

<abbotype>lite</abbotype>

<ispid>80000049</ispid>

<subscriberid>5108</subscriberid>

<voipaccounts>

<voipaccount type="main">

<telnumber>0501234567</telnumber>

<callbundle>evening</callbundle>

</voipaccount>

<voipaccount type="extra">

<telnumber>0502345678</telnumber>

<callbundle>always</callbundle>

</voipaccount>

<voipaccount type="nomadic">

<telnumber>0843553431</telnumber>

<callbundle>evening</callbundle>

</voipaccount>

<voipaccount type="nomadic">

<telnumber>0843553645</telnumber>

<callbundle>evening</callbundle>

</voipaccount>

</voipaccounts>

<DualPlay_CC_Manager_UserReadResponse>

<DualPlay_CC_Manager_UserReadResult>

<CallReferenceID>

M2Y4NWM3OjEwNmM0YTk1NTJjOi03ZjE3</CallReferenceID>

<ResultCode>2.0.0</ResultCode>

<ResultDescription>Success.</ResultDescription>

<AccountID>80001409</AccountID>

<BankAccount>1234567</BankAccount>

<BankAccountName>P. Paulussen</BankAccountName>

<BankAccountCity/>

<PrimaryMailboxID>260261</PrimaryMailboxID>

<FreeMailboxThreshold>1</FreeMailboxThreshold>

<MailboxCount>3</MailboxCount>

<Mailboxes>

<Mailbox>

<MailboxID>260261</MailboxID>

<AccountName>ipbtest150</AccountName>

</Mailbox>

<Mailbox>

<MailboxID>258325</MailboxID>

<AccountName>ipbtest150A</AccountName>

</Mailbox>

<Mailbox>

<MailboxID>259221</MailboxID>

<AccountName>ipbtest150B</AccountName>

</Mailbox>

</Mailboxes>

</DualPlay_CC_Manager_UserReadResult>

</DualPlay_CC_Manager_UserReadResponse>

</dualplay>

</dualplays>

</location>

</locations>

</vt>

<mt>

<roles>

<role>06012</role>

<role>02012</role>

<role>02033</role>

<role>06013</role>

<role>01013</role>

<role>01033</role>

<role>01012</role>

<role>02013</role>

<role>01032</role>

<role>06032</role>

<role>06033</role>

<role>02032</role>

</roles>

</mt>

</profile>

I work with JAXB 1.0. The class that implements the Element interface is org.apache.xerces.dom.DeferredElementNSImpl. (I used two statements in Profile2User to check this, because in forums I have noticed this could be a problem). We have xerces-2.4.0.jar.

Pease help me, because it has taken me already a day or three!!

[22316 byte] By [Hans_Posta] at [2007-10-2 6:21:51]
# 1

It looks like a couple of things are happening. I did a validation on your XML instance document against the Schema and it threw up a couple of errors. For example, the <roles> element appears in the instance document before the <ckr> element and there抯 no <dualplaytype> element. There may be some more errors further down.

Also your program seems to be mixing JAXB and DOM. Is that intentional because one of the beauties of JAXB is that you don抰 have to get down into all those tricky DOM methods. Below is a small program outline that I use as a starter for my JAXB stuff. I抳e renamed your instance and schema documents as forum.xml and forum.xsd respectively and bundled the generated classes into a package called extract.

package sandbox;

import java.io.*;

import java.util.*;

import javax.xml.bind.*;

import extract.*;

public class Profile2User {

// JAXB workers.

private static Unmarshaller unmarshaller;

private static JAXBContext profileJAXBContext;

private static Profile profile;

public static void main(String[] args) {

try {

createUnmarshaller();

profile = (Profile) unmarshaller.unmarshal(new FileInputStream( "forum.xml" ));

// Now, start processing the document.

} catch (JAXBException e) {

e.printStackTrace();

} catch (FileNotFoundException fnf) {

fnf.printStackTrace();

}

}

private static void createUnmarshaller() throws JAXBException {

System.out.println("Creating the unmarshaller.");

profileJAXBContext = JAXBContext.newInstance("extract");

unmarshaller = profileJAXBContext.createUnmarshaller();

unmarshaller.setValidating(true);

}

}

At the moment this program is throwing exceptions because of the validation errors, but once you get through those, you should be ready to start processing your document.

craigcaulfielda at 2007-7-16 13:23:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Hi Craig

Thank you for your response. I was not able to reply sooner than today, sorry for that.

You were right that the xml did not validate with the acoompanying xsd. I put the wrong xsd on the forum. In the end of this message you will find the right one. This one does validate in Xmlspy here. But I still got the same NullPointerException. I looked up the source of DomScanner and found out that in this code the NullPointerException is caused by the fact that the line

final NamedNodeMap attributes = e.getAttributes();

in this code gives null as result, and therefore the line

int len = attributes.getLength();

in the same code, a few lines later, delivers the NullPointerException.

When i study the code of DomScanner further, I see that an xmlns attribute is expected. The xml that I get via an interface with some tool, does not have this attribute, so I decided to add it in my code by adding the following line:

root.setAttribute("xmlns", "http://www.xxxxxx.com/2003/Dialogmanager");

(I put xxxxxx in stead of the name of the company for privay rasons)

Now i get the following error:

code = null melding = javax.xml.bind.UnmarshalException

- with linked exception:

[org.xml.sax.SAXParseException: unexpected root element (uri:"http://www.xxxxxx.com/2003/DialogManager", local:"profile"). Expected elements are <{}profile>]

org.xml.sax.SAXParseException: unexpected root element (uri:"http://www.xxxxxx.com/2003/DialogManager", local:"profile"). Expected elements are <{}profile>

at com.kpn.alk.dispatcher.jaxb.impl.runtime.SAXUnmarshallerHandlerImpl.startElement(SAXUnmarshallerHandlerImpl.java:114)

at org.xml.sax.helpers.XMLFilterImpl.startElement(Unknown Source)

at com.sun.xml.bind.unmarshaller.InterningXMLReader.startElement(InterningXMLReader.java:74)

at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:177)

at com.sun.xml.bind.unmarshaller.DOMScanner.parse(DOMScanner.java:72)

Can anyone help me on this? I see that profile is expected, and profile is found (local)? Was it right for me to add the xmlns attribute?

PS: I had put some DOM code in the example to see what the xml looked like.

The right xsd is as follows:

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:isp="http://dualplay.xxxxxx.com/webservices/isp/DualPlay_CC_Manager_UserRead">

<xs:element name="profile">

<xs:annotation>

<xs:documentation>Profile View: xxxxxxxx.COM user authorization profile: fixed and mobile</xs:documentation>

</xs:annotation>

<xs:complexType>

<xs:sequence>

<xs:element name="errors" minOccurs="0">

<xs:complexType>

<xs:sequence>

<xs:element name="error" type="error" maxOccurs="unbounded"/>

</xs:sequence>

</xs:complexType>

</xs:element>

<xs:element name="vt" type="vtType" minOccurs="0">

<xs:annotation>

<xs:documentation>VT View</xs:documentation>

</xs:annotation>

</xs:element>

<xs:element name="mt" type="mtType" minOccurs="0">

<xs:annotation>

<xs:documentation>MT View</xs:documentation>

</xs:annotation>

</xs:element>

<xs:element name="subaut" type="subautType" minOccurs="0">

<xs:annotation>

<xs:documentation>Subaut View: IAM subauthorizations</xs:documentation>

</xs:annotation>

</xs:element>

<xs:element name="personalized" type="personalizedType" minOccurs="0">

<xs:annotation>

<xs:documentation>Personalizations</xs:documentation>

</xs:annotation>

</xs:element>

</xs:sequence>

</xs:complexType>

</xs:element>

<xs:complexType name="error">

<xs:annotation>

<xs:documentation>Error during authorizations retrieval</xs:documentation>

</xs:annotation>

<xs:sequence>

<xs:element name="dynamictext" type="xs:string"/>

<xs:element name="errorcode" type="xs:unsignedShort"/>

<xs:element name="message" type="xs:string"/>

</xs:sequence>

</xs:complexType>

<xs:complexType name="mtType">

<xs:annotation>

<xs:documentation>Type of mobile authorisation</xs:documentation>

</xs:annotation>

<xs:sequence>

<xs:element name="roles" type="rolesType"/>

<xs:sequence minOccurs="0">

<xs:element name="loyalty"/>

<xs:element name="customer-id" type="xs:string"/>

</xs:sequence>

</xs:sequence>

</xs:complexType>

<xs:complexType name="vtType">

<xs:annotation>

<xs:documentation>Type of fixed authorisation</xs:documentation>

</xs:annotation>

<xs:sequence>

<xs:element name="roles" type="rolesType"/>

<xs:element name="ckr" type="xs:string"/>

<xs:element name="locations" type="locationsType">

<xs:annotation>

<xs:documentation>DualPlay View</xs:documentation>

</xs:annotation>

</xs:element>

</xs:sequence>

</xs:complexType>

<xs:complexType name="rolesType">

<xs:annotation>

<xs:documentation>List of roles</xs:documentation>

</xs:annotation>

<xs:sequence>

<xs:element name="role" type="xs:unsignedShort" maxOccurs="unbounded"/>

</xs:sequence>

</xs:complexType>

<xs:complexType name="locationsType">

<xs:annotation>

<xs:documentation>Locations for installed base</xs:documentation>

</xs:annotation>

<xs:sequence>

<xs:element name="location" type="locationType" minOccurs="0" maxOccurs="unbounded"/>

</xs:sequence>

<xs:attribute name="selecteddualplay" type="xs:string"/>

</xs:complexType>

<xs:complexType name="locationType">

<xs:annotation>

<xs:documentation>Location for installed base</xs:documentation>

</xs:annotation>

<xs:sequence>

<xs:element name="streetname" type="xs:string"/>

<xs:element name="housenumber" type="xs:unsignedShort"/>

<xs:element name="housenumberext" type="xs:string"/>

<xs:element name="postalcode" type="xs:string"/>

<xs:element name="cityname" type="xs:string"/>

<xs:element name="dualplays" type="dualPlaysType"/>

</xs:sequence>

</xs:complexType>

<xs:complexType name="dualPlaysType">

<xs:annotation>

<xs:documentation>Dual play products</xs:documentation>

</xs:annotation>

<xs:sequence>

<xs:element name="dualplay" type="dualPlayType" maxOccurs="unbounded"/>

</xs:sequence>

</xs:complexType>

<xs:complexType name="dualPlayType">

<xs:annotation>

<xs:documentation>Dual play product</xs:documentation>

</xs:annotation>

<xs:sequence>

<xs:element name="dualplayid" type="xs:string"/>

<xs:element name="serviceid" type="xs:string"/>

<xs:element name="initialized" type="xs:boolean"/>

<xs:element name="abbotype" type="abboType"/>

<xs:element name="ispid" type="xs:string"/>

<xs:element name="subscriberid" type="xs:string"/>

<xs:element name="voipaccounts" type="voipaccountsType"/>

<xs:choice>

<xs:choice>

<xs:element name="DualPlay_CC_Manager_UserReadResponse" type="UserResponseType" minOccurs="0"/>

<xs:element name="fasttrackerror" minOccurs="0"/>

<xs:element name="planetinternethetnet" minOccurs="0"/>

</xs:choice>

</xs:choice>

</xs:sequence>

</xs:complexType>

<xs:complexType name="UserResponseType">

<xs:sequence>

<xs:element name="DualPlay_CC_Manager_UserReadResult" type="UserReadType" minOccurs="0"/>

</xs:sequence>

</xs:complexType>

<xs:complexType name="UserReadType">

<xs:sequence>

<xs:element name="CallReferenceID" type="xs:string"/>

<xs:element name="ResultCode" type="xs:string"/>

<xs:element name="ResultDescription" type="xs:string"/>

<xs:element name="AccountID" type="xs:string"/>

<xs:element name="BankAccount" type="xs:string"/>

<xs:element name="BankAccountName" type="xs:string"/>

<xs:element name="BankAccountCity" type="xs:string"/>

<xs:element name="PrimaryMailboxID" type="xs:string"/>

<xs:element name="FreeMailboxThreshold" type="xs:string"/>

<xs:element name="MailboxCount" type="xs:string"/>

<xs:element name="Mailboxes">

<xs:complexType>

<xs:sequence>

<xs:element name="Mailbox" maxOccurs="unbounded">

<xs:complexType>

<xs:sequence>

<xs:element name="MailboxID" type="xs:string"/>

<xs:element name="AccountName" type="xs:string"/>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:sequence>

</xs:complexType>

<xs:simpleType name="abboType">

<xs:restriction base="xs:string">

<xs:enumeration value="go"/>

<xs:enumeration value="lite"/>

<xs:enumeration value="basic"/>

<xs:enumeration value="extra"/>

</xs:restriction>

</xs:simpleType>

<xs:simpleType name="phaseType">

<xs:restriction base="xs:string">

<xs:enumeration value="aangelegd"/>

<xs:enumeration value="in opheffing"/>

</xs:restriction>

</xs:simpleType>

<xs:complexType name="voipaccountsType">

<xs:sequence>

<xs:element name="voipaccount" type="voipaccountType" maxOccurs="unbounded"/>

</xs:sequence>

</xs:complexType>

<xs:complexType name="voipaccountType">

<xs:sequence>

<xs:element name="telnumber" type="xs:string"/>

<xs:element name="callbundle" type="callBundleType"/>

</xs:sequence>

<xs:attribute name="type" type="accountType"/>

</xs:complexType>

<xs:simpleType name="callBundleType">

<xs:restriction base="xs:string">

<xs:enumeration value="night"/>

<xs:enumeration value="evening"/>

<xs:enumeration value="always"/>

</xs:restriction>

</xs:simpleType>

<xs:simpleType name="accountType">

<xs:restriction base="xs:string">

<xs:enumeration value="main"/>

<xs:enumeration value="extra"/>

<xs:enumeration value="nomadic"/>

</xs:restriction>

</xs:simpleType>

<xs:complexType name="subautType">

<xs:annotation>

<xs:documentation>Type for subauthorizations from Anox IAM</xs:documentation>

</xs:annotation>

<xs:sequence>

<xs:element name="service" type="xs:string" maxOccurs="unbounded"/>

<xs:choice minOccurs="0">

<xs:element name="phone" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>

<xs:element name="mail" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>

</xs:choice>

</xs:sequence>

</xs:complexType>

<xs:complexType name="personalizedType">

<xs:annotation>

<xs:documentation>Type for personalizations</xs:documentation>

</xs:annotation>

<xs:sequence>

<xs:element name="row" type="rowType" minOccurs="0" maxOccurs="unbounded"/>

<xs:element name="resultset" type="xs:string"/>

</xs:sequence>

</xs:complexType>

<xs:complexType name="rowType">

<xs:annotation>

<xs:documentation>Type for personalizations rows</xs:documentation>

</xs:annotation>

<xs:sequence>

<xs:element name="column" type="columnType" maxOccurs="unbounded"/>

</xs:sequence>

<xs:attribute name="rownum" type="xs:unsignedShort"/>

</xs:complexType>

<xs:complexType name="columnType">

<xs:annotation>

<xs:documentation>Type for personalizations columns</xs:documentation>

</xs:annotation>

<xs:simpleContent>

<xs:extension base="xs:string">

<xs:attribute name="name" type="xs:string"/>

<xs:attribute name="type" type="xs:string"/>

</xs:extension>

</xs:simpleContent>

</xs:complexType>

</xs:schema>

Hans_Posta at 2007-7-16 13:23:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

Perhaps the DOM code mixed with JAXB is causing the problem. Using just JAXB, I started processing the XML instance document with the following program and all seemed OK.

package sandbox;

import java.io.*;

import java.util.*;

import javax.xml.bind.*;

import extract.*;

public class Profile2User {

// JAXB workers.

private static Unmarshaller unmarshaller;

private static JAXBContext profileJAXBContext;

private static Profile profile;

public static void main(String[] args) {

try {

createUnmarshaller();

profile = (Profile) unmarshaller.unmarshal(new FileInputStream( "forum.xml" ));

// Now, start processing the document.

System.out.println("Ckr value = " + profile.getVt().getCkr());

System.out.println("\nProcessing roles:");

List roles = profile.getVt().getRoles().getRole();

for (Iterator iterator = roles.iterator(); iterator.hasNext();) {

System.out.println("Role record = " + (Integer) iterator.next());

}

System.out.println("\nProcessing locations:");

List location = profile.getVt().getLocations().getLocation();

for (Iterator iterator = location.iterator(); iterator.hasNext();) {

LocationType thisLocation = (LocationType) iterator.next();

System.out.println("Street name = " + thisLocation.getStreetname());

System.out.println("House number = " + thisLocation.getHousenumber());

System.out.println("House number ext = " + thisLocation.getHousenumberext());

System.out.println("Postal code = " + thisLocation.getPostalcode());

System.out.println("City name = " + thisLocation.getCityname());

// And so on...

}

} catch (JAXBException e) {

e.printStackTrace();

} catch (FileNotFoundException fnf) {

fnf.printStackTrace();

}

}

private static void createUnmarshaller() throws JAXBException {

System.out.println("Creating the unmarshaller.");

profileJAXBContext = JAXBContext.newInstance("extract");

unmarshaller = profileJAXBContext.createUnmarshaller();

unmarshaller.setValidating(true);

}

}

Why not drop the DOM code and stick to pure JAXB?

craigcaulfielda at 2007-7-16 13:23:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

I have good news. I got JAXB to work now!!

The problem I had, had to do with proper use of namespaces.

The xml I get from outside, already has a namespace URI ("http://www.xxxxxx.com/2003/DialogManager"), while my XSD had no namespace at all. This explains the error I had:

unexpected root element (uri:"http://www.xxxxxx.com/2003/DialogManager", local:"profile"). Expected elements are <{}profile>]

I added the following lines to my xsd:

xmlns="http://www.xxxxxx.com/2003/DialogManager"

targetNamespace="http://www.xxxxxx.com/2003/DialogManager"

elementFormDefault="qualified">

and this did the job! I needed the elementFormDefault to be qualified, because without this, only the root element was recognized (<profile> in my case), and the unmarshalling went wrong at the first child element. In qualified mode, the xjc binder checks all elements to the targetNameSpace I added, and that worked fine!

Craig thanx very much for your help. I appreciated it! AFter all I deleted nearly all DOM code, and the left code looks like this now:

public void parse() throws KPNException {

try {

JAXBContext jc = JAXBContext.newInstance(

"com.kpn.alk.dispatcher.jaxb", this.getClass()

.getClassLoader());

Unmarshaller unmarshaller = jc.createUnmarshaller();

unmarshaller.setValidating(true);

if (LOG.isDebugEnabled()) showNode(profileNode, 0);

Node profileChild = profileNode.getFirstChild();

String name = profileChild.getNodeName();

if (name.equals(PROFILE_ELEMENT)) {

LOG.info("Starting unmarshalling profile ");

Profile profile = (Profile) unmarshaller.unmarshal(profileChild);

LOG.info("Finished unmarshalling profile ");

convertProfile(profile);

}

} catch (Exception e) {

throw new KPNTechnicalException("Umarshalling of profile failed", e);

}

}

There is still some logging in it, a.o. for showing the xml in the logging, but you will get the point. The convertProfile method still has to be written, to convert the JAXB objects into our own business objects. In the end we plan to use the JAXB objects directly as our business objcets, to avoid more converting after the JAXB unmarshalling.

Hans Post

Senior Software Engineer

Atos Origin NL

Hans_Posta at 2007-7-16 13:23:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...