使用绑定< xsd:any>创建对象到XML给空? [英] creating objects using binding <xsd:any> to xml is giving null?

查看:51
本文介绍了使用绑定< xsd:any>创建对象到XML给空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用从xml文件到从架构文件xsd生成的类的绑定数据来创建对象,但给出的是空值.

I am trying to create objects using binding data from xml file to classes which has generated from schema file xsd but it is giving 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

有人可以帮我....

推荐答案

   <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包含文本内容.

means that element name may contain any XML element. No wonder you're getting null, because XML contains text content.

一种解决方案可能是将模式更改为此:

One solution may be to change schema to this:

<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").这样您将获得:

(mind mixed="true"). This way you'll get:

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

在解组后,您将获得:

不能选择更改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>

解组后,您将得到:

这篇关于使用绑定&lt; xsd:any&gt;创建对象到XML给空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆