Friday, December 2, 2011

XML document structures must start and end within the same entity.

If you get below mentioned error

[Fatal Error] :1:7717: XML document structures must start and end within the same entity.
Exception in thread "main" org.xml.sax.SAXParseException: XML document structures must start and end within the same entity.
    at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:124)

As the error suggest XML document is not formatted properly. Kindly go through the XML and look for any missing tags.

Indenting XML Output in Java

Here is a simple JAVA program for Indenting / Formatting a long XML string. You can provide a XML as a string OR can provide XML from a file according to your requirement.

Formatting an XML can be useful for reading or just simply pretty printing it.

Here is a complete tested code you can directly run the program and see the output.

/* IndentXML.java */

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class IndentXML {

    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, TransformerException
    {
       
        //get the factory
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
       
        //Using factory get an instance of document builder
        DocumentBuilder db = dbf.newDocumentBuilder();
       
        //providing XML string to be indented
       
String xmlStringToIndent = "<AllEmployee><Employee><Name>Ram</Name><Designation>Programmer</Designation><Department>Development</Department></Employee><Employee><Name>Shyam</Name><Designation>TL</Designation><Department>Finanace</Department></Employee><Employee><Name>Geeta</Name><Designation>Manager</Designation><Department>HR</Department></Employee></AllEmployee>";
       
        //converting String to InputStream
        InputStream inStr = new ByteArrayInputStream(xmlStringToIndent.getBytes());
       
        Document doc = db.parse(inStr);
       
        //Output file
        File outputFile = new File("c:/indentedOutputXmlFile.xml");
        OutputStream outSrc = new FileOutputStream(outputFile);
       
        //creating a transformer
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer  = transFactory.newTransformer();
       
        transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5");
       
        transformer.transform(new DOMSource(doc), new StreamResult(outSrc));
       
        System.out.println("OutputFile created sucessfully!! "+outputFile.getPath());

    }

}


Input XML

String xmlStringToIndent = "<AllEmployee><Employee><Name>Ram</Name><Designation>Programmer</Designation><Department>Development</Department></Employee><Employee><Name>Shyam</Name><Designation>TL</Designation><Department>Finanace</Department></Employee><Employee><Name>Geeta</Name><Designation>Manager</Designation><Department>HR</Department></Employee></AllEmployee>";


Output XML

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<AllEmployee>
     <Employee>
          <Name>Ram</Name>
          <Designation>Programmer</Designation>
          <Department>Development</Department>
     </Employee>
     <Employee>
          <Name>Shyam</Name>
          <Designation>TL</Designation>
          <Department>Finanace</Department>
     </Employee>
     <Employee>
          <Name>Geeta</Name>
          <Designation>Manager</Designation>
          <Department>HR</Department>
     </Employee>
</AllEmployee>



 Note :

If you are passing XML string and it contains double quotes ("). Make to to bypass it by adding backward slash (\").

I observer below mentioned error when the string contained space in the beginning as show below.

String xmlStringToIndent = " <AllEmployee><Employee><Name>Ram</Name><Designation>Programmer ....."

Note the space in the beginning of the string. Make sure your XML is formatted properly.

Error

[Fatal Error] :1:7: The processing instruction target matching "[xX][mM][lL]" is not allowed.
Exception in thread "main" org.xml.sax.SAXParseException: The processing instruction target matching "[xX][mM][lL]" is not allowed.
    at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:124)
    at com.hewitt.hre.fadv.IndentXML.main(IndentXML.java:43)

Friday, November 25, 2011

SQLSTATE=42903

 I was in need to pass a max value in where condition. My folly, what i did is :

Incorrect : Select * from table_name where field = MAX(field);




Got below mentioned error in DB2

  42903(-120)[IBM][CLI Driver][DB2/SUN64] SQL0120N  Invalid use of an aggregate function or OLAP function.  SQLSTATE=42903
 (0.47 secs)

Correct :

Correct way is Inline Query

Select * from table_name where files = (Select MAX(filed) from table_name);

SQLSTATE=42601

 I was in need to fetch top n rows from a particular table in DB2. I got below mentioned error when tried with some incorrect syntax.

 42601(-104)[IBM][CLI Driver][DB2/SUN64] SQL0104N  An unexpected token "1" was found following "Select TOP ".  Expected tokens may include:  "".  SQLSTATE=42601
 (0.45 secs)

Correct Syntax :

Select * from table_name where filed = 'Some condition' order by field FETCH FIRST 10 ROWS ONLY;

SQLSTATE=42807

I got below mentioned error while updating a View in DB2.

 42807(-150)[IBM][CLI Driver][DB2/SUN64] SQL0150N  The target fullselect, view, typed table, materialized query table, or staging table in the INSERT, DELETE, UPDATE, or MERGE statement is a target for which the requested operation is not permitted.  SQLSTATE=42807

As the error suggest administrator have not given update permission to the user. Ask administrator to give access, else it would not be possible.