2012-07-10 51 views
2

有沒有任何Android的輕量級庫,就像桌面上的JAXB一樣? 給一個XML模式,創建代碼來解析,驗證,操作,然後再寫入它。 這些文件已經存在,並且因爲它是一個財務應用程序,所以不得修改任何未修改的文件。包括空格,排序和字符編碼。 (我在做這個多年,JAXB,它工作正常,但我不能端口的代碼由於使用Android缺乏JAXB和它的足跡。)JAXB替代Android的XML架構?

回答

1

您可以嘗試Simple FrameworkCastor

+0

我已查看簡單。他們不接受也不驗證XML模式。 – 2012-07-10 10:53:50

+0

蓖麻看起來不錯。我沒有看到任何看起來像Android的show-stoppers的依賴。謝謝! – 2012-07-10 10:58:30

0

以下爲我工作驗證

  1. 創建一個驗證工具。
  2. 在android操作系統上同時獲取xml和xsd文件,並使用驗證實用程序來對付它。
  3. 使用Xerces-For-Android進行驗證。

的Android不支持一些軟件包,我們可以使用,我創建了一個基於我的XML驗證工具:http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/validation/package-summary.html

我最初的沙盒測試是用java相當順利,然後我試圖將它移植過來的Dalvik和發現我的代碼不起作用。有些事情與Dalvik並不相同,所以我做了一些修改。

我發現的xerces的Android的引用,所以我修改的沙箱試驗(下面不與機器人工作,在此之後的實例中確實):

import java.io.File; 

import javax.xml.XMLConstants; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.transform.Source; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamSource; 
import javax.xml.validation.Schema; 
import javax.xml.validation.SchemaFactory; 
import javax.xml.validation.Validator; 

import org.w3c.dom.Document; 

/** 
* A Utility to help with xml communication validation. 
*/ 
public class XmlUtil { 

    /** 
    * Validation method. 
    * Base code/example from: http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/validation/package-summary.html 
    * 
    * @param xmlFilePath The xml file we are trying to validate. 
    * @param xmlSchemaFilePath The schema file we are using for the validation. This method assumes the schema file is valid. 
    * @return True if valid, false if not valid or bad parse. 
    */ 
    public static boolean validate(String xmlFilePath, String xmlSchemaFilePath) { 

     // parse an XML document into a DOM tree 
     DocumentBuilder parser = null; 
     Document document; 

     // Try the validation, we assume that if there are any issues with the validation 
     // process that the input is invalid. 
     try { 
      // validate the DOM tree 
      parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
      document = parser.parse(new File(xmlFilePath)); 

      // create a SchemaFactory capable of understanding WXS schemas 
      SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 

      // load a WXS schema, represented by a Schema instance 
      Source schemaFile = new StreamSource(new File(xmlSchemaFilePath)); 
      Schema schema = factory.newSchema(schemaFile); 

      // create a Validator instance, which can be used to validate an instance document 
      Validator validator = schema.newValidator(); 
      validator.validate(new DOMSource(document)); 
     } catch (Exception e) { 
      // Catches: SAXException, ParserConfigurationException, and IOException. 
      return false; 
     }  

     return true; 
    } 
} 

上面的代碼有修改一些與android的xerces(http://gc.codehum.com/p/xerces-for-android/)一起使用。你需要SVN拿到項目,下面是一些嬰兒牀筆記:

download xerces-for-android 
    download silk svn (for windows users) from http://www.sliksvn.com/en/download 
     install silk svn (I did complete install) 
     Once the install is complete, you should have svn in your system path. 
     Test by typing "svn" from the command line. 
     I went to my desktop then downloaded the xerces project by: 
      svn checkout http://xerces-for-android.googlecode.com/svn/trunk/ xerces-for-android-read-only 
     You should then have a new folder on your desktop called xerces-for-android-read-only 

通過上述罐子(最後我會讓它變成一個罐子,只是複製它直接進入我的源快速測試如果您希望做同樣的,你可以用Ant使罐子很快(http://ant.apache.org/manual/using.html)),我是能夠得到以下爲我的XML驗證工作:

import java.io.File; 
import java.io.IOException; 

import mf.javax.xml.transform.Source; 
import mf.javax.xml.transform.stream.StreamSource; 
import mf.javax.xml.validation.Schema; 
import mf.javax.xml.validation.SchemaFactory; 
import mf.javax.xml.validation.Validator; 
import mf.org.apache.xerces.jaxp.validation.XMLSchemaFactory; 

import org.xml.sax.SAXException; 

/** 
* A Utility to help with xml communication validation. 
*/public class XmlUtil { 

    /** 
    * Validation method. 
    * 
    * @param xmlFilePath The xml file we are trying to validate. 
    * @param xmlSchemaFilePath The schema file we are using for the validation. This method assumes the schema file is valid. 
    * @return True if valid, false if not valid or bad parse or exception/error during parse. 
    */ 
    public static boolean validate(String xmlFilePath, String xmlSchemaFilePath) { 

     // Try the validation, we assume that if there are any issues with the validation 
     // process that the input is invalid. 
     try { 
      SchemaFactory factory = new XMLSchemaFactory(); 
      Source schemaFile = new StreamSource(new File(xmlSchemaFilePath)); 
      Source xmlSource = new StreamSource(new File(xmlFilePath)); 
      Schema schema = factory.newSchema(schemaFile); 
      Validator validator = schema.newValidator(); 
      validator.validate(xmlSource); 
     } catch (SAXException e) { 
      return false; 
     } catch (IOException e) { 
      return false; 
     } catch (Exception e) { 
      // Catches everything beyond: SAXException, and IOException. 
      e.printStackTrace(); 
      return false; 
     } catch (Error e) { 
      // Needed this for debugging when I was having issues with my 1st set of code. 
      e.printStackTrace(); 
      return false; 
     } 

     return true; 
    } 
} 

一些旁註:

用於創建fi LES,我做了一個簡單的文件工具寫字符串的文件:

public static void createFileFromString(String fileText, String fileName) { 
    try { 
     File file = new File(fileName); 
     BufferedWriter output = new BufferedWriter(new FileWriter(file)); 
     output.write(fileText); 
     output.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

我還需要寫信給我不得不進入一個區域,所以我利用了:

String path = this.getActivity().getPackageManager().getPackageInfo(getPackageName(), 0).applicationInfo.dataDir; 

小哈克,它的作品。我確信有這樣一種更簡潔的方式,但我想我會分享我的成功,因爲我沒有找到任何好的例子。