CLEiM
Cross Lingual Education in Medicine
 All Classes Namespaces Files Functions Variables
MyDOMParser.java
Go to the documentation of this file.
00001 package com.uem.gsi.cleim.util;
00002 
00003 import javax.xml.parsers.*;
00004 import org.w3c.dom.*;
00005 import org.xml.sax.InputSource;
00006 
00007 import java.io.*;
00008 
00009 public class MyDOMParser implements java.io.Serializable {
00010    public MyDOMParser() {
00011    }
00019    public static Document getDocument(String url, int method) throws Exception {
00020          
00021          // Step 1: create a DocumentBuilderFactory
00022      DocumentBuilderFactory df = DocumentBuilderFactory.newInstance();
00023 
00024      // Step 2: create a DocumentBuilder
00025      //df.setNamespaceAware(true);
00026      DocumentBuilder db = df.newDocumentBuilder();
00027      
00028      // Step 3: parse the input file to get a Document object
00029      Document doc;
00030      if (method==1){
00031          File file = new File(url);
00032          InputStream inputStream= new FileInputStream(file);
00033          Reader reader = new InputStreamReader(inputStream,"UTF-8");
00034          InputSource is = new InputSource(reader);
00035          is.setEncoding("UTF-8");
00036          doc = db.parse(is);
00037      }else
00038          doc = db.parse(url);
00039      return doc;
00040    }
00041    public static Document getDocument(String xml) throws Exception {
00042      
00043          // Step 1: create a DocumentBuilderFactory
00044      DocumentBuilderFactory df = DocumentBuilderFactory.newInstance();
00045 
00046      // Step 2: create a DocumentBuilder
00047      //df.setNamespaceAware(true);
00048      DocumentBuilder db = df.newDocumentBuilder();
00049      // Step 3: parse the input text to get a Document object
00050      InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
00051      Document doc = db.parse(is);
00052      
00053      return doc;
00054    }
00055 }