Tag Archives: xml

Jaxb UnMarshall without namespace

To unmarshal (get object) from xml:
1. XSD must be:




See: elementFormDefault=”qualified” attributeFormDefault=”unqualified” (!)

2. For converting in usual case, for ex. file1.xml:



    
        
            
            
            
            
        
    

(!) xmlns is present.

Java code:

File file = new File("file1.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Fier.class.getPackage().getName());
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Fier fier = (Fier)jaxbUnmarshaller.unmarshal(file);

3. If you want to suppress xmlns, for ex:



    
        
            
            
            
            
        
    

You have to use the following code:

String sXml = "get file1.xml contents...";
sXml = sXml.trim().replaceFirst("^([\\W]+)<", "<");

InputSource is = new InputSource(new StringReader(sXml));
XMLReader reader = XMLReaderFactory.createXMLReader();

// Namespace URIs and local names to the unmarshaller needs to be interned.
// http://stackoverflow.com/questions/4375176/how-to-ignore-casing-of-element-names-in-jaxb
// Solution: .intern()
String ns = new String("http://my.com/some").intern();

NameSpaceFilter filter = new NameSpaceFilter(ns, true);
filter.setParent(reader);
SAXSource source = new SAXSource(filter, is);

JAXBContext jc = JAXBContext.newInstance(Fier.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();

unmarshaller.setEventHandler(new ValidationEventHandler()
{
	public boolean handleEvent(ValidationEvent event)
	{
		return false;
	}
});
Fier fier = (Fier)unmarshaller.unmarshal(source);

And next class:

/**
* @see http://dev64.wordpress.com/2012/04/26/namespace-filter-example/
*/
private class NameSpaceFilter extends XMLFilterImpl
{
	private String usedNamespaceUri;
	private boolean isAddNamespace;

	// State variable
	private boolean isAddedNamespaceAlready = false;

	public NameSpaceFilter(String namespaceUri, boolean addNamespace)
	{
		super();

		if (addNamespace)
			usedNamespaceUri = namespaceUri;
		else
			usedNamespaceUri = "";

		isAddNamespace = addNamespace;
	}

	@Override
	public void startDocument() throws SAXException
	{
		super.startDocument();
		if (isAddNamespace)
		{
			startControlledPrefixMapping();
		}
	}

	@Override
	public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException
	{
		super.startElement(usedNamespaceUri, arg1, arg2, arg3);
	}

	@Override
	public void endElement(String arg0, String arg1, String arg2) throws SAXException
	{
		super.endElement(usedNamespaceUri, arg1, arg2);
	}

	@Override
	public void startPrefixMapping(String prefix, String url) throws SAXException
	{
		if (isAddNamespace)
		{
			startControlledPrefixMapping();
		}
		else
		{
			// Remove the namespace, i.e. don?t call startPrefixMapping for parent!
		}
	}

	private void startControlledPrefixMapping() throws SAXException
	{
		if (isAddNamespace && !isAddedNamespaceAlready)
		{
			// We should add namespace since it is set and has not yet been done.
			super.startPrefixMapping("", usedNamespaceUri);

			// Make sure we dont do it twice
			isAddedNamespaceAlready = true;
		}
	}
}

So, we explicitly add namespace to each element of input xml data.

C#

Find node in XML with NameSpace


Beginers C# parse XML manually :)
Using XPath is more efficiently, like this:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(data[0].RowID);

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("ns1", xmlDoc.DocumentElement.NamespaceURI);

XmlNode nodeEmail = xmlDoc.SelectSingleNode("/ns1:feed/ns1:author/ns1:email", nsmgr);
XmlNode nodeUserID = xmlDoc.SelectSingleNode("/ns1:feed/ns1:entry/ns1:id", nsmgr);
XmlNode nodeUserName = xmlDoc.SelectSingleNode("/ns1:feed/ns1:entry/ns1:title", nsmgr);

For more interest, use XPathVisualizer

http://stackoverflow.com/questions/585812/using-xpath-with-default-namespace-in-c
http://mydotnet.wordpress.com/2008/05/29/worlds-smallest-xml-xpath-tutorial/