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);

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

Greate code snippet from


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.
// Solution: .intern()
String ns = new String("").intern();

NameSpaceFilter filter = new NameSpaceFilter(ns, true);
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
private class NameSpaceFilter extends XMLFilterImpl
	private String usedNamespaceUri;
	private boolean isAddNamespace;

	// State variable
	private boolean isAddedNamespaceAlready = false;

	public NameSpaceFilter(String namespaceUri, boolean addNamespace)

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

		isAddNamespace = addNamespace;

	public void startDocument() throws SAXException
		if (isAddNamespace)

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

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

	public void startPrefixMapping(String prefix, String url) throws SAXException
		if (isAddNamespace)
			// 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.