【问题标题】:JAXB unmarshalling in detailJAXB 详细解组
【发布时间】:2016-08-02 17:47:28
【问题描述】:

我是 jaxb 用法的新手,我能够将带有 xml 注释的 java 类编组为 .xml,但无法在解组时检索数据。当我对未编组的数据执行 sysout 时,它会打印上下文的地址而不是实际值。我不确定我哪里出错了。

<collections>
    <collectionclass="testclass">
        <group>
            <header code="T123" type="toys"/>
            <obj1 location="1" shelf="4" />
            <obj2 location="7" shelf="2"  count="3"/>
                <associations>
                    <association type="String" associatedName="train" associatedFieldSize="0"/>
                    <association type="DataLength" associatedName="ship" associatedFieldSize="0"/>
                </associations>
            </obj2>
         </group>
           <collectionclass="testclass">
  </collections>

我还想了解更多关于解组 xml 文档时生成的“jaxb 上下文”和“java 模型/java 模型类”等术语以及它是如何形成的。

提前致谢!

【问题讨论】:

    标签: java xml jaxb unmarshalling


    【解决方案1】:

    尝试如下。

    试试{

        File file = new File("C:\\file.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
    
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);
        System.out.println(customer);
    
      } catch (JAXBException e) {
        e.printStackTrace();
      }
    

    【讨论】:

    • 我试过这样的东西,JAXBContext jaxbContext = JAXBContext.newInstance(Collections.class);解组器 jaxbUnmarshaller = jaxbContext.createUnmarshaller();集合collectionContext =(集合)jaxbUnmarshaller.unmarshal(新文件(“TestXml.xml”)); System.out.println(collectionContext);输出:com.eaxmple.package.test.Collections@63753b6d
    • 看起来您正在尝试打印对象。您应该打印该对象中的属性,然后只有您可以看到这些值。 System.out.println(customer.name);
    猜你喜欢
    • 2018-08-30
    • 2014-10-05
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多