Person Nodes

Sep 27, 2012 at 5:44 PM

I am trying to get the "Emergency or Provider Contact" with JAXB. I can get the information without a problem, but then I try to convert it into a Person object and it fails. Here is the XML being returned:

<response><status><code>0</code></status><wc:info xmlns:wc="urn:com.microsoft.wc.methods.response.GetThings3"><group><thing><thing-id version-stamp="c0d11ecc-104b-4d96-aa80-22bff6d606de">f67b2d22-8cb5-483a-859c-a830e2353484</thing-id><type-id name="Emergency or Provider Contact">25c94a9f-9d3d-4576-96dc-6791178a8143</type-id><data-xml><person><name><full>Dr. FirstName MiddleName LastName</full><title><text>Dr.</text><code><value>Dr</value><family>wc</family><type>name-prefixes</type><version>1</version></code></title><first>FirstName</first><middle>MiddleName</middle><last>LastName</last></name><organization>PlaceOfBusiness</organization><professional-training>Speciality</professional-training><contact><address><description>Business</description><is-primary>true</is-primary><street>123 Business Ln.</street><street>Line2</street><street>Line3</street><city>MyCity</city><state>MyState</state><postcode>99999</postcode><country>United States</country><county>MyCounty</county></address><address><description>Personal</description><is-primary>false</is-primary><street>123 Personal Ln.</street><street>Line2</street><street>Line3</street><city>MyCity</city><postcode>88888</postcode><country>Afghanistan</country></address><phone><description>Business</description><is-primary>true</is-primary><number>000-000-0000</number></phone><phone><description>Home</description><is-primary>false</is-primary><number>111-111-1111</number></phone><email><description>Business</description><is-primary>true</is-primary><address>email@email.email</address></email><email><description>Other</description><is-primary>false</is-primary><address>second@second.second</address></email></contact><type><text>Emergency Contact</text><code><value>1</value><family>wc</family><type>person-types</type><version>1</version></code></type></person><common><note>MyNote</note></common></data-xml></thing><thing><thing-id version-stamp="512a12da-bb19-4e30-8ac6-6eb18891a5bd">8ac09819-56c9-4961-9400-7ac02cb6894b</thing-id><type-id name="Emergency or Provider Contact">25c94a9f-9d3d-4576-96dc-6791178a8143</type-id><data-xml><person><name><full>Dr. Seuss</full><title><text>Dr.</text><code><value>Dr</value><family>wc</family><type>name-prefixes</type><version>1</version></code></title><last>Seuss</last></name></person><common /></data-xml></thing></group></wc:info></response>

 

And here is the code:

// Create the XPath object to use to query the document.
XPath xPath = XPathFactory.newInstance().newXPath();
		
// Get the medication XML tags.
NodeList nodes;
try {
	nodes = 
		(NodeList) xPath
			.evaluate(
				"//person",
				document, 
				XPathConstants.NODESET);
}
catch(XPathExpressionException e) {
	throw
		new IllegalArgumentException(
			"The XPath expression is invalid.",
			e);
}

// Create the unmarshaller, which will take the XML and convert it into
// type-specific objects.
JAXBContext jc = JAXBContext.newInstance(Person.class);
Unmarshaller u = jc.createUnmarshaller();

// For each of the nodes, convert it into its type-specific object.
int numNodes = nodes.getLength();
List<Person> results = new ArrayList<Person>(numNodes);
for(int i = 0; i < numNodes; i++) {
	results.add((Person) u.unmarshal(nodes.item(i)));
}

// Return the list of results.
return results;

 

And here is the exception.

Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"person"). Expected elements are (none)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:648)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:236)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:231)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:105)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1051)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:484)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:465)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:60)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:135)
	at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:228)
	at com.sun.xml.internal.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:111)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:305)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:288)
	at test.HealthVault.getConditions(HealthVault.java:389)
	at test.HealthVault.main(HealthVault.java:53)

The Person class does not have a @XmlRootElement annotation, which I believe is the problem. I am not that familiar with JAXB, but should I be doing this in a different way?

Coordinator
Sep 28, 2012 at 7:06 AM

You're right.  I fixed it and added a test for the Contact type under com/microsoft/hsg/jaxb/things/ContactTest.  If you check out the latest src from the trunk you can see the test and get the fixed up code to make it all work.

--Rob

Sep 28, 2012 at 5:59 PM

Worked like a charm! Thank you very much.

John