2013-04-14 74 views
1

我創建了一個XSD,並且我的一個元素屬性名爲value。我使用JAXB創建類,但是當我使用這些類創建XML時value不是屬性,它是元素標記中的值。JAXB未將值屬性視爲屬性

XSD:

<?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" xmlns:sel="http://ibm.org/seleniumframework"/> 

    <xs:complexType name="Test"> 
     <xs:choice minOccurs="1" maxOccurs="unbounded"> 
      <xs:element type="sel:CLISessionType" name="CLISession" xmlns:sel="http://ibm.org/seleniumframework"/> 
      <xs:element type="sel:DBSessionType" name="DBSession" xmlns:sel="http://ibm.org/seleniumframework"/> 
      <xs:element type="sel:ScreensType" name="Screens" xmlns:sel="http://ibm.org/seleniumframework"/> 
     </xs:choice> 
    </xs:complexType> 

    <!--Screen XML section --> 
    <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 type="sel:ScreenDataType" name="ScreenData" minOccurs="1" maxOccurs="unbounded" xmlns:sel="http://ibm.org/seleniumframework"/> 
     </xs:sequence> 
     <xs:attribute type="xs:string" name="name" use="required" /> 
     <xs:attribute type="xs:string" name="package" use="required" /> 
     <xs:attribute type="xs:string" name="class" use="required" /> 
    </xs:complexType> 

    <xs:complexType name="ScreenDataType"> 
     <xs:sequence> 
      <xs:element type="sel:elementType" name="element" 
       minOccurs="1" maxOccurs="unbounded" xmlns:sel="http://ibm.org/seleniumframework"/> 
     </xs:sequence> 
     <xs:attribute type="xs:int" name="step" use="required"/> 
     <xs:attribute type="xs:string" name="description" use="required"/> 
    </xs:complexType> 

    <xs:complexType name="elementType"> 
     <xs:simpleContent> 
      <xs:extension base="xs:string"> 
       <xs:attribute type="xs:string" name="name" use="required" /> 
       <xs:attribute type="xs:string" name="type" use="required" /> 
       <xs:attribute type="xs:string" name="value" use="required" /> 
      </xs:extension> 
     </xs:simpleContent> 
    </xs:complexType> 

    <!--CLI XML section --> 
    <xs:complexType name="CLISessionType"> 
     <xs:sequence> 
      <xs:element type="sel:CliLoginType" name="login" 
       minOccurs="1" maxOccurs="1" xmlns:sel="http://ibm.org/seleniumframework"/> 
      <xs:element type="sel:CommandsType" name="Commands" 
       minOccurs="1" maxOccurs="1" xmlns:sel="http://ibm.org/seleniumframework"/> 
     </xs:sequence> 
     <xs:attribute type="xs:string" name="use_test.properties" 
      use="required" /> 
    </xs:complexType> 

    <xs:complexType name="CliLoginType"> 
     <xs:simpleContent> 
      <xs:extension base="xs:string"> 
       <xs:attribute type="xs:string" name="host" use="required" /> 
       <xs:attribute type="xs:string" name="password" use="required" /> 
       <xs:attribute type="xs:string" name="username" use="required" /> 
      </xs:extension> 
     </xs:simpleContent> 
    </xs:complexType> 

    <xs:complexType name="CommandsType"> 
     <xs:sequence> 
      <xs:element type="sel:CommandType" name="Command" 
       minOccurs="1" maxOccurs="unbounded" xmlns:sel="http://ibm.org/seleniumframework"/> 
     </xs:sequence> 
    </xs:complexType> 

    <xs:complexType name="CommandType"> 
     <xs:simpleContent> 
      <xs:extension base="xs:string"> 
       <xs:attribute type="xs:string" name="exe" use="required"/> 
       <xs:attribute type="xs:string" name="args" use="required"/> 
       <xs:attribute type="xs:string" name="wait" use="required"/> 
       <xs:attribute type="xs:string" name="expectedOutput" use="required"/> 
       <xs:attribute type="xs:string" name="toVariable" use="required"/> 
      </xs:extension> 
     </xs:simpleContent> 
    </xs:complexType> 

    <!--DB XML section --> 
    <xs:complexType name="DBSessionType"> 
     <xs:sequence> 
      <xs:element type="sel:DbLoginType" name="login" minOccurs="1" 
       maxOccurs="1" xmlns:sel="http://ibm.org/seleniumframework"/> 
      <xs:element type="sel:QueriesType" name="Queries" 
       minOccurs="1" maxOccurs="1" xmlns:sel="http://ibm.org/seleniumframework"/> 
     </xs:sequence> 
     <xs:attribute type="xs:string" name="use_test.properties" 
      use="required" /> 
     <xs:attribute type="xs:string" name="use_dbserver" use="required"/> 
     <xs:attribute type="xs:string" name="use_db" use="required"/> 
    </xs:complexType> 

    <xs:complexType name="DbLoginType"> 
     <xs:simpleContent> 
      <xs:extension base="xs:string"> 
       <xs:attribute type="xs:string" name="server" use="required"/> 
       <xs:attribute type="xs:string" name="database" use="required"/> 
       <xs:attribute type="xs:string" name="username" use="required"/> 
       <xs:attribute type="xs:string" name="password" use="required"/> 
      </xs:extension> 
     </xs:simpleContent> 
    </xs:complexType> 

    <xs:complexType name="QueriesType"> 
     <xs:sequence> 
      <xs:element type="sel:SQLType" name="SQL" maxOccurs="unbounded" 
       minOccurs="1" xmlns:sel="http://ibm.org/seleniumframework"/> 
     </xs:sequence> 
    </xs:complexType> 

    <xs:complexType name="SQLType"> 
     <xs:simpleContent> 
      <xs:extension base="xs:string"> 
       <xs:attribute type="xs:string" name="query" use="required"/> 
       <xs:attribute type="xs:string" name="expectedResults" use="required"/> 
      </xs:extension> 
     </xs:simpleContent> 
    </xs:complexType> 

</xs:schema> 

綁定文件:

<bindings xmlns="http://java.sun.com/xml/ns/jaxb" 
      xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" 
      xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      version="2.1"> 
    <bindings schemaLocation="xmlSchema.xsd" version="1.0"> 
     <!-- Customise the package name --> 
     <schemaBindings> 
      <package name="com.example.schema"/> 
     </schemaBindings> 

     <!-- rename the value element --> 
     <bindings node=".//xs:attribute[@name='value']"> 
      <property name="ValueAttribute"/> 
     </bindings> 
    </bindings> 
</bindings> 

JAXB命令:

xjc -b xmlSchema.xjb -d C:\Users\Alison\workspace\FTFXmlGenerator\src -p com.q1labs.qa.xmlgenerator.model.generatedxmlclasses xmlSchema.xsd

如何修改這個使單元內的值作爲處理屬性?

編輯:

package com.qa.xmlgenerator.model.generatedxmlclasses; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlType; 
import javax.xml.bind.annotation.XmlValue; 


/** 
* <p>Java class for elementType complex type. 
* 
* <p>The following schema fragment specifies the expected content contained within this class. 
* 
* <pre> 
* &lt;complexType name="elementType"> 
* &lt;simpleContent> 
*  &lt;extension base="&lt;http://www.w3.org/2001/XMLSchema>string"> 
*  &lt;attribute name="name" use="required" type="{http://www.w3.org/2001/XMLSchema}string" /> 
*  &lt;attribute name="type" use="required" type="{http://www.w3.org/2001/XMLSchema}string" /> 
*  &lt;attribute name="value" use="required" type="{http://www.w3.org/2001/XMLSchema}string" /> 
*  &lt;/extension> 
* &lt;/simpleContent> 
* &lt;/complexType> 
* </pre> 
* 
* 
*/ 
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "elementType", propOrder = { 
    "value" 
}) 
public class ElementType { 

    @XmlValue 
    protected String value; 
    @XmlAttribute(name = "name", required = true) 
    protected String name; 
    @XmlAttribute(name = "type", required = true) 
    protected String type; 
    @XmlAttribute(name = "value", required = true) 
    protected String valueAttribute; 

    /** 
    * Gets the value of the value property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getValue() { 
     return value; 
    } 

    /** 
    * Sets the value of the value property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setValue(String value) { 
     this.value = value; 
    } 

    /** 
    * Gets the value of the name property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getName() { 
     return name; 
    } 

    /** 
    * Sets the value of the name property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setName(String value) { 
     this.name = value; 
    } 

    /** 
    * Gets the value of the type property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getType() { 
     return type; 
    } 

    /** 
    * Sets the value of the type property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setType(String value) { 
     this.type = value; 
    } 

    /** 
    * Gets the value of the valueAttribute property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getValueAttribute() { 
     return valueAttribute; 
    } 

    /** 
    * Sets the value of the valueAttribute property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setValueAttribute(String value) { 
     this.valueAttribute = value; 
    } 

} 
+0

u能張貼xmlSchema.xsd和xmlSchema.xjb整個代碼?含義來自''。 – Jops

+0

更新,是你需要的一切嗎? – Colin747

+0

您的問題是「當我使用這些類創建XML值不是屬性時,它是元素標記內的值。」在生成的* ElementType.java *中,您是否看到如下所示:'@XmlAttribute(name =「value」,required = true)protected String valueAttribute;',或者其他不同的東西?你能分享那些代碼嗎? – Jops

回答

1

既然你在你的自定義綁定改名valueAttribute,你應該使用重命名屬性以及設定值:

element.setValueAttribute("valueAttribute"); 

我想這我自己和價值結束了一個屬性。

ElementType element = new ElementType(); 
element.setName("name"); 
element.setType("type"); 
element.setValueAttribute("valueAttribute"); 

你應該得到:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<elementType value="valueAttribute" type="type" name="name"/> 
+1

一直在爲此工作了很長時間,謝謝:) – Colin747