使用绑定<xsd:any>创建对象到xml是否为null?</xsd:any>

时间:2012-06-28 06:48:26

标签: java xml xsd jaxb

我正在尝试使用xml文件中的绑定数据创建对象,这些对象是从模式文件xsd生成的,但是它给出了null。

这是我的xsd,我从中生成了我的java类:

  <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

     <xsd:element name="people">
        <xsd:complexType>
           <xsd:sequence>
               <xsd:element ref="employee"/>
               <xsd:element ref="customer"/>
           </xsd:sequence>
         </xsd:complexType>
       </xsd:element>

      <xsd:element name="employee">
     <xsd:complexType>
             <xsd:sequence>
                     <xsd:element ref='name'/>
                     <xsd:element ref='country'/>
              </xsd:sequence>
         </xsd:complexType>
      </xsd:element>

       <xsd:element name='name'>
               <xsd:complexType>
                  <xsd:sequence>
                        <xsd:any namespace='http://www.w3.org/namespace/'/>
                   </xsd:sequence>
              </xsd:complexType>
       </xsd:element>

       <xsd:element name='country'>
             <xsd:complexType>
                 <xsd:sequence>
                     <xsd:any namespace='http://www.w3.org/namespace/'/>
                 </xsd:sequence>
              </xsd:complexType>
       </xsd:element>

       <xsd:element name="customer">
            <xsd:complexType>
            <xsd:sequence>
                    <xsd:element ref='cname'/>
                </xsd:sequence>
            </xsd:complexType>
       </xsd:element>

       <xsd:element name='cname'>
             <xsd:complexType>
                     <xsd:sequence>
                          <xsd:any namespace='http://www.w3.org/namespace/'/>
                     </xsd:sequence>
              </xsd:complexType>
        </xsd:element>
    </xsd:schema>  

我的XML文件:

      <?xml version="1.0" encoding="UTF-8"?>
       <people>
           <employee>
              <name>John</name>            
              <country>India</country>
           </employee>
           <customer>
              <cname>steve</cname>            
           </customer>
       </people>

这里我的代码尝试将xml数据绑定到java对象,但是给出了null:

 File file = new File("D:\\file.xml");  
 JAXBContext jaxbContext = JAXBContext.newInstance("com.jaxb.xmlbinding");  
 Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
  People element = (People) jaxbUnmarshaller.unmarshal(file);  
 System.out.println(element.getEmployee().getName().getAny()); //giving null

有人可以帮助我......

1 个答案:

答案 0 :(得分:5)

   <xsd:element name='name'>
           <xsd:complexType>
              <xsd:sequence>
                    <xsd:any namespace='http://www.w3.org/namespace/'/>
               </xsd:sequence>
          </xsd:complexType>
   </xsd:element>

表示元素name可能包含任何XML 元素。难怪你得到null,因为XML包含 text 内容。

一种解决方案可能是将架构更改为:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:element name="people">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="employee" />
        <xsd:element ref="customer" />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <xsd:element name="employee">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref='name' />
        <xsd:element ref='country' />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <xsd:element name='name'>
    <xsd:complexType mixed="true">
      <xsd:sequence>
        <xsd:any namespace='http://www.w3.org/namespace/' />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <xsd:element name='country'>
    <xsd:complexType mixed="true">
      <xsd:sequence>
        <xsd:any namespace='http://www.w3.org/namespace/' />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <xsd:element name="customer">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref='cname' />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <xsd:element name='cname'>
    <xsd:complexType mixed="true">
      <xsd:sequence>
        <xsd:any namespace='http://www.w3.org/namespace/' />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

(记住mixed="true")。这样你就会得到:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "content"
})
@XmlRootElement(name = "name")
public class Name {

    @XmlMixed
    @XmlAnyElement(lax = true)
    protected List<Object> content;

而不是:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "any"
})
@XmlRootElement(name = "name")
public class Name {

    @XmlAnyElement(lax = true)
    protected Object any;

在解组之后,你会得到:

Eclipse's debug view

编辑:更改XSD的情况不是一个选项

这是有效的XML:

<?xml version="1.0" encoding="UTF-8"?>
<people xmlns:p1="http://www.w3.org/namespace/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema2.xsd">
  <employee>
    <name>
      <p1:a>x</p1:a>
    </name>
    <country>
      <p1:a>x</p1:a>
    </country>
  </employee>
  <customer>
    <cname>
      <p1:a>x</p1:a>
    </cname>
  </customer>
</people>

然后在解组后你会得到:

enter image description here

相关问题