2012-08-27 71 views
3

我在使用JAXB從XSD創建XML文件時遇到問題,下面是用於創建它的XSD文件。 (注:名稱已經被編輯過,由於保密)從JAXB的XSD創建XML文件

<?xml version="1.0" encoding="UTF-8"?> 
    <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://ibm.org/seleniumframework" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

    <xs:element name="Test" type="sel:Test"> 
     <xs:complexType> 
      <xs:choice minOccurs="1" maxOccurs="unbounded"> 
       <xs:element name="Option1" type="sel:Option1Type" xmlns:sel="http://ibm.org/seleniumframework"/> 
       <xs:element name="Option2" type="sel:Option2Type" xmlns:sel="http://ibm.org/seleniumframework"/> 
       <xs:element name="Option3" type="sel:ScreensType" xmlns:sel="http://ibm.org/seleniumframework"/> 
      </xs:choice> 
     </xs:complexType> 
    </xs:element> 

    <xs:complexType name="ScreensType"> 
     <xs:sequence> 
      <xs:element type="sel:ScreenType" name="Screen" minOccurs="1" maxOccurs="unbounded" xmlns:sel="http://ibm.org/seleniumframework"/> 
     </xs:sequence> 
    </xs:complexType> 

    <xs:complexType name="ScreenType"> 
     <xs:sequence> 
      <xs:element name="ScreenData" minOccurs="1" maxOccurs="unbounded" xmlns:sel="http://ibm.org/seleniumframework"/> 
     </xs:sequence> 
     <xs:attribute type="xs:string" name="name1" use="required" /> 
     <xs:attribute type="xs:string" name="name2" use="required" /> 
     <xs:attribute type="xs:string" name="name3" use="required" /> 
    </xs:complexType> 

</xs:schema> 

這是我使用的嘗試和創建XML代碼:

public void generateXml() throws JAXBException, IOException { 

      Test test = new Test(); 
      ScreensType screens = new ScreensType(); 
      ScreenType screen = new ScreenType(); 
      screen.setName1("a"); 
      screen.setName2("b"); 
      screen.setName3("c"); 

      File f = new File("new.xml"); 
      JAXBContext context= JAXBContext.newInstance("com.q1labs.qa.xmlgenerator.model.generatedxmlclasses"); 
      Marshaller jaxbMarshaller = context.createMarshaller(); 
      jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 

      jaxbMarshaller.marshal(test, f); 
      jaxbMarshaller.marshal(test, System.out); 

     } 

這是輸出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
     <Test xmlns="http://ibm.org/seleniumframework"/> 

我怎樣才能讓代碼輸出屏幕和屏幕標記以及它的屬性,我不知道我在做什麼錯誤。

+1

根據你的代碼,你幾乎不會創建'screen'和'screens'變量,也不會對它們做任何事情。你只是測試。你忘了'test.setOption3(屏幕)'或類似的東西? –

回答

4

您創建了一個TestScreensType的實例,但在將它們編組爲XML之前從未設置它們的任何屬性。以下是更正後的代碼。

public void generateXml() throws JAXBException, IOException { 
    Test test = new Test(); 
    ScreensType screens = new ScreensType(); 
    test.getOption1OrOption2OrOption3().add(screens); 
    ScreenType screen = new ScreenType(); 
    screen.setName1("a"); 
    screen.setName2("b"); 
    screen.setName3("c"); 
    screens.getScreen().add(screen); 

    File f = new File("new.xml"); 
    JAXBContext context= JAXBContext.newInstance("com.q1labs.qa.xmlgenerator.model.generatedxmlclasses"); 
    Marshaller jaxbMarshaller = context.createMarshaller(); 
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 

    jaxbMarshaller.marshal(test, f); 
    jaxbMarshaller.marshal(test, System.out); 
} 

輸出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<Test xmlns="http://ibm.org/seleniumframework"> 
    <Option3> 
     <Screen name1="a" name2="b" name3="c"/> 
    </Option3> 
</Test> 
+1

工作正常!非常感謝!我是JAXB的新手,所以你提供了很多幫助。 – Colin747

+1

@ Colin747 - 我很高興我能幫上忙。順便說一句,我維護一個Java XML和JSON綁定博客,你可能會覺得有用:http://blog.bdoughan.com/ –

+1

乾杯,爲書籤! – Colin747

2

嘗試了幾天後,最終我能創建XSD正確使用下面給出的代碼的XML。希望這會有所幫助:

  String filename = "<filepath/filename>"; 

      final Document doc = loadXsdDocument(filename); 

     //Find the docs root element and use it to find the targetNamespace 
      final Element rootElem = doc.getDocumentElement(); 
      String targetNamespace = null; 
      if (rootElem != null && rootElem.getNodeName().equals("xs:schema")) 
         { 
       targetNamespace = rootElem.getAttribute("targetNamespace"); 
      } 


         //Parse the file into an XSModel object 
      XSModel xsModel = new XSParser().parse(filename); 

         //Define defaults for the XML generation 
      XSInstance instance = new XSInstance(); 
      instance.minimumElementsGenerated = 1; 
      instance.maximumElementsGenerated = 1; 
      instance.generateDefaultAttributes = true; 
      instance.generateOptionalAttributes = true; 
      instance.maximumRecursionDepth = 0; 
      instance.generateAllChoices = true; 
      instance.showContentModel = true; 
      instance.generateOptionalElements = true; 

         //Build the sample xml doc 
         //Replace first param to XMLDoc with a file input stream to write to file 
      QName rootElement = new QName(targetNamespace, "<xsd file name>"); 
      XMLDocument sampleXml = new XMLDocument(new StreamResult(System.out), true, 4, null); 
      instance.generate(xsModel, rootElement, sampleXml); 
     } catch (TransformerConfigurationException e) 
       { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    public static Document loadXsdDocument(String inputName) { 
     final String filename = inputName; 

     final DocumentBuilderFactory factory = DocumentBuilderFactory 
       .newInstance(); 
     factory.setValidating(false); 
     factory.setIgnoringElementContentWhitespace(true); 
     factory.setIgnoringComments(true); 
     Document doc = null; 

     try { 
      final DocumentBuilder builder = factory.newDocumentBuilder(); 
      final File inputFile = new File(filename); 
      doc = builder.parse(inputFile); 
     } catch (final Exception e) { 
      e.printStackTrace(); 
      // throw new ContentLoadException(msg); 
     } 

     return doc; 
    } 
+0

此答案的示例代碼使用[JLibs項目](https://santhosh-tekuri.github.io/jlibs/xsd/XSInstance.html)中的類。 –