Anywhere you go, let me go too

關於部落格
對人海闊天空,做事仔細周密
----------------------
因為改了平台後...覺得不是很好用....所以有另外......(評估中)
http://blog.xuite.net/king119wang/myskills
  • 32543

    累積人氣

  • 2

    今日人氣

    0

    訂閱人氣

Convert Xml to Xsd and Parse XML in Java

1. Convert Xml to Xsd File
  請直接看下面這篇文章說明,差別只是他是用unix系統中,就自行轉為bat即可
  http://www.thaiopensource.com/relaxng/trang.html
2.XSD To Java Object
  和.NET 一樣要轉程該程式的Object , 作法如下:
(1) 下載JAXB的元件
   http://jaxb.java.net/2.2.4-1/
   依該網站說明
   解jar物件
   java -jar JAXB2_20110601.jar  ===> 裏面有doc / sample code/ api 
(2) 設定
在說明中,有下面這一段,就照作囉!
 
The Java TM Architecture for XML Binding (JAXB) provides an API and tools that automate the mapping between XML documents and Java objects. 

Schema-2-Java is the process of compiling one or more schema files into generated Java classes. Here are some of the basic steps for developing an app: 

1.Develop/locate your schema 
2.Annotate the schema with binding customizations if necessary (or place them in an external bindings file) 
3.Compile the schema with the XJC binding compiler 
4.Develop your JAXB client application using the Java content classes generated by the XJC binding compiler along with the javax.xml.bind runtime framework 
   
==============================針對上面這一段,補一下說明
        java -jar %JAXB_HOME%libjaxb-xjc.jar -help   (可以用這個語法看xjc怎麼帶參數)
C:jaxb-ri-20110601bin>java -jar %JAXB_HOME%libjaxb-xjc.jar -xmlschema D:WORKSkillJAXBbook.xsd D:WORK待研究SkillJAXBTestBook
parsing a schema...
compiling a schema...
generatedBookCategoryType.java
generatedBookType.java
generatedCollection.java
generatedObjectFactory.java

        另外,在 JAXB2_20110601.jar 解壓後, 可以在bin目錄中找到一個xjc.bat的檔,也可以直接用此在Dos 模式下執行也可以
 
5.Set your CLASSPATH to include all of the JAR files 
   jaxb-api.jar, jaxb-impl.jar, jsr173_1.0_api.jar, activation.jar
6.Compile all of your Java sources with javac 
7.Run it!
===========================
Java doc還是要看的
http://jaxb.java.net/nonav/2.2.3u2/docs/api/

3. 調整轉好的Java Object ,撰寫組XML元件程式
    轉好的Java Object 有時會增加一些不必要的Object, 例如:某些Tag Text就只是純文字,實在沒必要用Object來處理, 
會用到的jaxb元件有下列幾項:
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
   其實就是XML層級觀念,再組物件時就是由最內層的物件開始建, then 外層物件包內層物件
   例:
     private ObjectFactory factory = new ObjectFactory();      ===>用此物件,去生下面所有物件
     private Root objRoot = null;
     private Bank objBank=null;
     private MQGW objMQGW=null;

                 factory=new ObjectFactory();     
objRoot = factory.createRoot();
objBank=factory.createBank();
objMQGW=factory.createMQGW();

   接下來,就是將上述每個物件的Value填入
     ex:
                objMQGW.setDealsId("0");
       objMQGW.setReturnCode(ReturnCode+"");
       objMQGW.setReturnDesc(ReturnDesc); 
  外層包內層
     ex:      objBank.setMQGW(objMQGW);          
 
   包到最上層後,就用Marshaller物件轉成XML Stream即可
 //轉為 XML格式 
 
       JAXBContext context = JAXBContext.newInstance(Root.class);
       Marshaller marshaller = context.createMarshaller();
       marshaller.setProperty(Marshaller.JAXB_ENCODING,"UTF-8");
       marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true); //是否生成xml字串
       marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false); //是否省略xml head訊息
       
       ByteArrayOutputStream objByteArrayOutputStream=new ByteArrayOutputStream();
       
       marshaller.marshal(objRoot,objByteArrayOutputStream);
       //將結果轉為字串及byte[]
       vbResultXML=objByteArrayOutputStream.toByteArray();
       sResultXml=objByteArrayOutputStream.toString();
       
       //System.out.print(sResultXml);
       //System.out.print(new String(vbResultXML));
    
4.拆XML檔
import javax.xml.bind.JAXBException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
原理類似,就是反向一個一個拆回來
private JAXBContext context=null;
private Unmarshaller objUnmarshaller=null;
private Root objRecvRespReqSendFMRoot=null;
private Bank objBank=null;
private MQGW objMQGW=null;

              context=JAXBContext.newInstance(Root.class);
objUnmarshaller=context.createUnmarshaller();
       objRecvRespReqSendFMRoot=(Root)objUnmarshaller.unmarshal(objInputStream);
       objBank=objRecvRespReqSendFMRoot.getBank();
       objMQGW=objBank.getMQGW();   ===> 取後內層後,就可以取值囉!

                System.out.println(objMQGW.getReturnCode());
       System.out.println(objMQGW.getReturnDesc());
       System.out.println(objMQGW.getSendDate());

 


相簿設定
標籤設定
相簿狀態