Monthly Archives: June 2012

Serialize data in Java

List list = Arrays.asList(new String[] { "A", "B", "C", "D" });
FileOutputStream fos = new FileOutputStream("list.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(list);
oos.close();

FileInputStream fis = new FileInputStream("list.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
List anotherList = (List) ois.readObject();
ois.close();

Greate code snippet from http://www.java2s.com/Tutorial/Java/0140__Collections/SerializingHashMaps.htm

SQL

Difference between SQL query restriction and join-restriction

For ex. you have simple join (Query 1):

SELECT * FROM table1
LEFT JOIN table2 ON table1.column1=table2.column2

and the other query is (Query 2):

SELECT * FROM table1,table2
WHERE table1.column1=table2.column2

What is the difference?

Query 1 gives you more records than Query 2. Case, in result will present ALL rows from table1 and columns of table2 = NULL
The second query results coinciding rows only.

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.