1

我创建了一个 XSD,我的元素属性之一被命名为value. 我使用 JAXB 创建了类,但是当我使用这些类创建 XMLvalue不是属性时,它是元素标记中的值。

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;
    }

}
4

1 回答 1

1

由于您在自定义绑定中将value重命名为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"/>
于 2013-04-15T16:31:19.937 回答