Jaxb

xjc -d D:\eclipse\workspace\JaxbTest\src -p "edu.jlu.xml" "D:\eclipse\workspace\JaxbTest\schema\messages.xsd"java

其中D:\eclipse\workspace\JaxbTest\src爲原文件的目錄
edu.jlu.xml爲生成Java類的包名
D:\eclipse\workspace\JaxbTest\schema\messages.xsd爲xml schema文件的路徑eclipse

 

public class Test {spa

 private static String filePath = "";
 private static SchemaFactory schFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
 private static javax.xml.validation.Schema sch;orm

 public static void main(String[] args) {
  try {
   sch = schFactory.newSchema(new File("D:\eclipse\workspace\JaxbTest\schema\messages.xsd"));
   toXml();
   toObj();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 private static void toXml() throws Exception {
 //
  JAXBContext jaxbContext = JAXBContext.newInstance(T.class);
  // marshal 整理,編列,元帥的意思
  Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
//  jaxbMarshaller.setSchema(sch);
  // format, make every element keep a separate line.
  jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);xml

  // output --> file
  File file = new File(filePath);
  jaxbMarshaller.marshal(object, file);
  
  // output --> console
  jaxbMarshaller.marshal(object, System.out);
 }
 
 private static void toObj() throws Exception {
  File file = new File(filePath);
  JAXBContext jaxbContext = JAXBContext.newInstance(T.class);
  Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
  Object scbml = jaxbUnmarshaller.unmarshal(file);
  System.out.println(scbml.getPayload().getPayloadContent());
 }ip

 }element

相關文章
相關標籤/搜索