如标题所示
如使用castor和xml的绑定,首先根据xsd文件生成javabean,然后就可以方便的使用javabean中的marshal和unmarshal 实现javabean和xml的互转。
但使用过程中,发现了这样一个问题,如果不同的PATH下  ,出现两个属性不同但Name相同的的complexType,如下图所示就会在一个包里面生成两个repeatModel 对象,而又没有重新生成package,这就导致了相互覆盖的现象。业务有特殊的要求,不可能重新命名,请各位高手出出主意。
xsd文件副本如下,将taxDoc.xsd和test.xsd放入同一个文件夹下
test.xsd代码如下
<?xml version="1.0" encoding="GB2312"?>
<!-- edited with XMLSpy v2009 (http://www.altova.com) by renmh (EMBRACE) -->
<xsd:schema xmlns="http://www.chinatax.gov.cn/dataspec/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.chinatax.gov.cn/dataspec/" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:include schemaLocation="taxDoc.xsd"/>
<xsd:complexType name="test">
<xsd:annotation>
<xsd:documentation>test</xsd:documentation>
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="taxDoc">
<xsd:sequence>
<xsd:element name="repeatModel">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="id" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="tybody">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="repeatModel">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="birthday"/>
<xsd:element name="age"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>taxDoc.xsd文件如下
<?xml version="1.0" encoding="GB2312"?>
<!-- edited with XMLSpy v2009 sp1 (http://www.altova.com) by xdowns.com (xdowns.com) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:complexType name="taxDoc" abstract="true">
<xs:attribute name="version" type="xs:string" use="optional" default="SW5001-2006"/>
<xs:attribute name="name" type="xs:string" use="required"/>
<xs:attribute name="cnName" type="xs:string" use="required"/>
</xs:complexType>
</xs:schema>castor生成bean的命令如下
java -classpath D:\xsd/castor-1.2-anttasks.jar;D:\xsd/castor-1.2-codegen.jar;D:\xsd/commons-logging-1.1.jar;D:\xsd/castor-1.2-ddlgen.jar;D:\xsd/castor-1.2-jdo.jar;D:\xsd/castor-1.2-xml-schema.jar;D:\xsd/castor-1.2-xml.jar;D:\xsd/castor-1.2.jar;D:\xsd/velocity-1.4.jar;D:\xsd/commons-logging.jar org.exolab.castor.builder.SourceGeneratorMain -types j2 -i "D:\xsd\test\taxMlhl-wsyw-tyRequest.xsd"   -package   test  -dest   "D:/xsd/test" -fcastorxsdxml绑定javabean

解决方案 »

  1.   

    castor生成javabean的方式,请参考
    http://download.csdn.net/detail/rmh1985/5338783
      

  2.   

    我是用maven-jaxb2-plugin插件帮你做了一下
    修改一下的内容就可以了1 将taxDoc.xsd改名字,不要是taxDoc因为和<xs:complexType name="taxDoc" 重复,有错误,例如
    将其改为taxDocs.xsd2 修改test.xsd中<xsd:include schemaLocation="taxDoc.xsd"/>
    将taxDoc.xsd改为taxDocs.xsd经过以上修改,maven 插件可以编译出对应的java bean..希望对你有帮助。
      

  3.   

    编译出来是没问题的,关键是出来的javabean中对象会相互覆盖
      

  4.   

    以下是生成的java 代码一个是Test.Tybody.RepeatModel
    一个Test.RepeatModel包名不一样,为什么会覆盖?
    package test;import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlType;@XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "test", propOrder = {
        "repeatModel",
        "tybody"
    })
    public class Test
        extends TaxDoc
    {    @XmlElement(required = true)
        protected Test.RepeatModel repeatModel;
        @XmlElement(required = true)
        protected Test.Tybody tybody;    /**
         * Gets the value of the repeatModel property.
         * 
         * @return
         *     possible object is
         *     {@link Test.RepeatModel }
         *     
         */
        public Test.RepeatModel getRepeatModel() {
            return repeatModel;
        }    /**
         * Sets the value of the repeatModel property.
         * 
         * @param value
         *     allowed object is
         *     {@link Test.RepeatModel }
         *     
         */
        public void setRepeatModel(Test.RepeatModel value) {
            this.repeatModel = value;
        }    /**
         * Gets the value of the tybody property.
         * 
         * @return
         *     possible object is
         *     {@link Test.Tybody }
         *     
         */
        public Test.Tybody getTybody() {
            return tybody;
        }    /**
         * Sets the value of the tybody property.
         * 
         * @param value
         *     allowed object is
         *     {@link Test.Tybody }
         *     
         */
        public void setTybody(Test.Tybody value) {
            this.tybody = value;
        }    @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "", propOrder = {
            "name",
            "id"
        })
        public static class RepeatModel {        @XmlElement(required = true)
            protected String name;
            @XmlElement(required = true)
            protected String id;        /**
             * 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 id property.
             * 
             * @return
             *     possible object is
             *     {@link String }
             *     
             */
            public String getId() {
                return id;
            }        /**
             * Sets the value of the id property.
             * 
             * @param value
             *     allowed object is
             *     {@link String }
             *     
             */
            public void setId(String value) {
                this.id = value;
            }    }    @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "", propOrder = {
            "repeatModel"
        })
        public static class Tybody {        @XmlElement(required = true)
            protected Test.Tybody.RepeatModel repeatModel;        /**
             * Gets the value of the repeatModel property.
             * 
             * @return
             *     possible object is
             *     {@link Test.Tybody.RepeatModel }
             *     
             */
            public Test.Tybody.RepeatModel getRepeatModel() {
                return repeatModel;
            }        /**
             * Sets the value of the repeatModel property.
             * 
             * @param value
             *     allowed object is
             *     {@link Test.Tybody.RepeatModel }
             *     
             */
            public void setRepeatModel(Test.Tybody.RepeatModel value) {
                this.repeatModel = value;
            }
            /**
             * <p>Java class for anonymous complex type.
             * 
             * <p>The following schema fragment specifies the expected content contained within this class.
             * 
             * <pre>
             * &lt;complexType>
             *   &lt;complexContent>
             *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
             *       &lt;sequence>
             *         &lt;element name="birthday" type="{http://www.w3.org/2001/XMLSchema}anyType"/>
             *         &lt;element name="age" type="{http://www.w3.org/2001/XMLSchema}anyType"/>
             *       &lt;/sequence>
             *     &lt;/restriction>
             *   &lt;/complexContent>
             * &lt;/complexType>
             * </pre>
             * 
             * 
             */
            @XmlAccessorType(XmlAccessType.FIELD)
            @XmlType(name = "", propOrder = {
                "birthday",
                "age"
            })
            public static class RepeatModel {            @XmlElement(required = true)
                protected Object birthday;
                @XmlElement(required = true)
                protected Object age;            /**
                 * Gets the value of the birthday property.
                 * 
                 * @return
                 *     possible object is
                 *     {@link Object }
                 *     
                 */
                public Object getBirthday() {
                    return birthday;
                }            /**
                 * Sets the value of the birthday property.
                 * 
                 * @param value
                 *     allowed object is
                 *     {@link Object }
                 *     
                 */
                public void setBirthday(Object value) {
                    this.birthday = value;
                }            /**
                 * Gets the value of the age property.
                 * 
                 * @return
                 *     possible object is
                 *     {@link Object }
                 *     
                 */
                public Object getAge() {
                    return age;
                }            /**
                 * Sets the value of the age property.
                 * 
                 * @param value
                 *     allowed object is
                 *     {@link Object }
                 *     
                 */
                public void setAge(Object value) {
                    this.age = value;
                }        }    }}
      

  5.   

    非常感谢!,总算知道了一种解决方法
    不过Castor好像是不知道动态生成包名