Showing posts with label Xml. Show all posts
Showing posts with label Xml. Show all posts

Saturday, 7 September 2013

org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0x13) , (Unicode: 0x9) was found in the element content of the document.


While read a xml getting org.xml.sax.SAXParseException the parser exception when parsing the xml. i check text that create this Exception nothing special character found there. but while dBuilder.parse() method parse xml throw exception .


log4j:WARN File option not set for appender [file].
log4j:WARN Are you using FileAppender instead of ConsoleAppender?
[Fatal Error] employeeInfo.xml:45:35: An invalid XML character (Unicode: 0x13) was found in the element content of the document.
Exception in thread "main" org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0x13) was found in the element content of the document.
 at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:246)
 at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
 at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:180)
 at javastoreroom.ReadXML.main(ReadXMLFile.java:33)


Spending a lots of hour while (open xml in OpenOffice find the reason like Name :Arun but when check in OpenOffice it's look like Aru#n content some junk character) finally get solution first read all xml content using BufferedReader and do this :-
String lineReader =" ";
while((lineReader = bufferedReader.readLine()) != null){
  lineReader = lineReader.replaceAll("[^\\x20-\\x7e]", "");
  buffer.append(l);
  }

Now create a temporary xml file and write buffer all content. Then read temp.xml now everything goes very well.Check it here valid or invalid character in xml

Hope this will help you :)


Monday, 2 September 2013

com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.


while reading another xml file using smb jcifs. Here i am refer this example read .xml file using smb jcifs . Xml contain some UTF-8 characters inside a XML file, and parser is not configure to parse the UTF-8 properly, characters like copyright , reserve etc. Throw an exception :-


com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
 at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:684)
 at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:554)
 at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1742)
 at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipChar(XMLEntityScanner.java:1416)
 at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2793)
 at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:647)
 at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
 at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
 at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
 at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
 at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:232)
 at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
 at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:124)
 at com.gwtech.source.XmlReader.readAllXML(XmlReader.java:61)
 at com.gwtech.source.XmlReader.execute(XmlReader.java:343)
 at org.quartz.core.JobRunShell.run(JobRunShell.java:191)
 at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:516)


To read content in UTF-8 format modify input source :-

SmbConnect.java class found Here Do some modification :-

Change this code :-
InputStream inputStream = sFile.getInputStream();
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(inputStream);
to This
InputStream stream = new SmbFileInputStream(fXmlFile);
  Reader reader = new InputStreamReader(stream);
  InputSource inputSource = new InputSource(reader);
  inputSource.setEncoding("UTF-8"); // set UTF-8 character encoding 

  DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
  Document doc = dBuilder.parse(inputSource);

Now Everything is working fine hope this help you :)

Sunday, 4 August 2013

read .xml file using smb jcifs , jcifs


We are already discuss Java CIFS Client Library here.


Now reading .xml file using smb Requirement:-
1. PersonDetails.xml reside in server or another machine


     
         Arun
         2009-08-21T08:43:02
         Kumar
       
      
         15000
         5000
      

2. Download jcifs-1.1.11.jar add in your lib folder

Constant.java
package com.javastoreroom.mytestapp;
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
    public class Constant {
  
    public static final String USER_NAME = "domain\\username";
    public static final String PASSWORD = "domain\\password";
    public static final String FILE_SOURCE_PATH = "smb://IP-Address/folderName/PersonDetails.xml"; // The local network's broadcast address of  target file or directory. SmbFile URLs
     
 
}

SmbConnect.java

package com.javastoreroom.mytestapp;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class SmbConnect {

 private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(SmbConnect.class);

 public static void main(String... args) {
  new SmbConnect().readXMLFiles();
 }

 public void readXMLFiles() {
  try {
   NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, Constant.USER_NAME, Constant.PASSWORD); //This class stores and encrypts NTLM user credentials.
   
   SmbFile sFile = new SmbFile(Constant.FILE_SOURCE_PATH , auth); //This class represents a resource on an SMB network.
          try {
       
           if(sFile.getName().endsWith(".xml")){
            
     InputStream inputStream = sFile.getInputStream();
     DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
     DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
     Document doc = dBuilder.parse(inputStream);
     doc.getDocumentElement().normalize();
     NodeList nList = doc.getElementsByTagName("person");            
            
     for (int temp = 0; temp < nList.getLength(); temp++) {
      Node nNode = nList.item(temp);
      if (nNode.getNodeType() == Node.ELEMENT_NODE) {
       Element eElement = (Element) nNode;
       
       log.info("|===================Here is person details===================|");
       
       log.info(" name -- >" + eElement.getElementsByTagName("name").item(0).getTextContent());
       log.info(" joining date -- > " + eElement.getElementsByTagName("join-date").item(0).getTextContent());
       log.info(" last name -- > " + eElement.getElementsByTagName("last-name").item(0).getTextContent());
       log.info(" basic salary -- > " + eElement.getElementsByTagName("basic").item(0).getTextContent());
       log.info(" hra allowance -- > " + eElement.getElementsByTagName("HRA").item(0).getTextContent());
       
       log.info("|===========================EOF=============================|");
       
      }
            
               }
              }
    } catch (Exception exception) {
    exception.printStackTrace();
   }
  }
  catch (Exception e) {
   e.printStackTrace();
  }
 }
 
}


after executing SmbConnect.java following output are shown :-


DONE :)

Saturday, 3 August 2013

org.xml.sax.SAXParseException: Premature end of file



[Fatal Error] :1:1: Premature end of file.
org.xml.sax.SAXParseException: Premature end of file.
 at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:246)
 at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
 at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:124)
 at com.javastoreroom.source.XmlReader.readAllXML(XmlReader.java:60)
 at com.javastoreroom.source.XmlReader.execute(XmlReader.java:342)
 at org.quartz.core.JobRunShell.run(JobRunShell.java:191)
 at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:516)

When i am reading a hundred of .xml file using a thread , getting this error ([Fatal Error] :1:1: Premature end of file.)
i am google to known try few other correction (like close InputStream , Reset ) but getting again & again .When i try to
know the reason check my first test.xml file it's empty & create error here i am check file size (fXmlFile.length()>0) everything
works fine.

Hope this help you and save your some time :)