Creating SAML objects from an XML source is done through a process known as unmarshalling which operates on DOM Elements. The OpenSAML 2 library contains many unmarshallers and the appropriate one is selected based off of either the XML Schema type of the Element to be unmarshalled or the Element's QName (a tuple representing the namespace the element is in and the element's name).
org.opensaml.Configuration#getUnmarshallerFactory()
org.opensaml.xml.io.UnmarshallerFactory#getUnmarshaller(Element)
method. The element argument is the root element from which you wish to begin unmarshalling. This is normally the document element if you're unmarshalling data from a file.unmarshall(Element)
method on the returned Unmarshaller
using the same element argument as in the previous step.The following example shows the parsing and unmarshalling of a SAML 2 metadata document. The try/catch code has been omitted here for readability. The complete code can be found in org.opensaml.saml2.metadata.MetadataTest#testInCommonUnmarshall()
method in the OpenSAML 2.0 test source code.
String inCommonMDFile = "/data/org/opensaml/saml2/metadata/InCommon-metadata.xml"; // Initialize the library DefaultBootstrap.bootstrap(); // Get parser pool manager BasicParserPool ppMgr = new BasicParserPool(); ppMgr.setNamespaceAware(true); // Parse metadata file InputStream in = MetadataTest.class.getResourceAsStream(inCommonMDFile); Document inCommonMDDoc = ppMgr.parse(in); Element metadataRoot = inCommonMDDoc.getDocumentElement(); // Get apropriate unmarshaller UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory(); Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(metadataRoot); // Unmarshall using the document root element, an EntitiesDescriptor in this case EntitiesDescriptor inCommonMD = (EntitiesDescriptor) unmarshaller.unmarshall(metadataRoot); |
xsi:type
attribute. If it does, the factory attempts to lookup an unmarshaller for that schema type. If the element does not have a specified schema type, or no unmarshaller is registered for that schema type, the factory uses the element's name.QName
argument using the getUnmarshaller(QName)
method. If this method is used the two step check mentioned above is not used, instead it is up to the developer to know, or care, whether the given QName represents an element's name or its schema type.