带有 JAXB 类的 GXT

Posted

技术标签:

【中文标题】带有 JAXB 类的 GXT【英文标题】:GXT with JAXB classes 【发布时间】:2010-09-09 18:05:11 【问题描述】:

我正在尝试创建一个用户界面,其中包含一个可编辑网格,其中包含来自 XSD 文件的数据。我已经使用 XJC 从 XSD 文件生成了所需的类。但我不知道如何使用 JAXB 生成类的方法将数据加载到网格中。谁能告诉我该怎么做?我已经使用 XSD 文件的 XML 文档实现了 Sencha 网站 (http://www.sencha.com/examples/pages/grid/xml.html) 的简单 XML 网格示例的代码。但是,我遇到了无法从 XML 文件加载内部子节点数据的问题。谁能帮我解决这两个问题?谢谢。

编辑:以下是我的网格代码。

public class XMLToGridex extends LayoutContainer


//@Override
protected void onRender(Element parent, int index)

    super.onRender(parent, index);
    setLayout(new FlowLayout(10));

    List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
    columns.add(new ColumnConfig("name", "Name", 100));
    columns.add(new ColumnConfig("frameworkFrequencyMultiplier", "FrameWorkFrequencyMultiplier", 300));
    columns.add(new ColumnConfig("additional", "Additional", 100));

    // create the column model  
    ColumnModel cm = new ColumnModel(columns);  

    // defines the xml structure  
    ModelType type = new ModelType();  
    type.setRoot("module");  
    type.setRecordName("emsElement");  
    type.addField("name");  
    type.addField("frameworkFrequencyMultiplier"); 
  // type.addField("convertItem");
   // type.setRoot("convertItem");
   // type.setRecordName("source");
    //type.addField("additional");
    //type.addField("converter");
    //type.addField("converter");
    //type.setRecordName(")
   // type.addField("convertItem");

    // use a http proxy to get the data  
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,  
        GWT.getHostPageBaseURL() + "module.xml");  
    HttpProxy<String> proxy = new HttpProxy<String>(builder);

    // need a loader, proxy, and reader  
    XmlLoadResultReader<ListLoadResult<ModelData>> reader = new XmlLoadResultReader<ListLoadResult<ModelData>>(  
        type);  

    final BaseListLoader<ListLoadResult<ModelData>> loader = new BaseListLoader<ListLoadResult<ModelData>>(  
            proxy, reader);  

    ListStore<ModelData> store = new ListStore<ModelData>(loader);  
    final Grid<ModelData> grid = new Grid<ModelData>(store, cm);  
    grid.setBorders(true);  
    grid.setLoadMask(true);  
    grid.getView().setEmptyText("Please hit the load button.");  
    //grid.setAutoExpandColumn("Name");
  //  grid.setAutoExpandColumn("");
    //grid.setAutoExpandColumn("ConvertItem");

    ContentPanel panel = new ContentPanel();  
    panel.setFrame(true);  
    panel.setCollapsible(true);  
    panel.setAnimCollapse(false);  
    panel.setButtonAlign(HorizontalAlignment.CENTER);  
    //panel.setIcon(Resources.ICONS.table());  
    panel.setHeading("XML Table Demo");  
    panel.setLayout(new FitLayout());  
    panel.add(grid);  
    panel.setSize(575, 350);  

    // add buttons  
    Button load = new Button("Load XML", new SelectionListener<ButtonEvent>()   
     public void componentSelected(ButtonEvent ce)   
        loader.load();  
        
    );  

    panel.addButton(load);  

    add(panel);  
    

Edit2:我也发布 XML 文档以获得更好的想法。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<module>
<emsElement>
    <name>c1</name>
    <frameworkFrequencyMultiplier>1</frameworkFrequencyMultiplier>
    <convertItem>
        <source user="test" url="https://192.168.103.153:8443/mi/xmlInterfaceRequest.do" passwd="837103af8990d948">
            <additional>method=post</additional>
            <useEncryptPasswd>true</useEncryptPasswd>
            <trustStoreLocation>$NH_HOME/custom/imframework/config/trustkeystore</trustStoreLocation>
            <trustStorePassword>changeit</trustStorePassword>
        </source>
        <processor deltaed="true" name="camiant" additional="requestName=All">
            <serverName>192.168.103.130</serverName>
        </processor>
        <converter name="dump">
            <additional>timestamp=true</additional>
            <location url="$NH_HOME/modules/imframework/fileStagingArea/dump"/>
        </converter>
        <converter name="element">
            <location url="$NH_HOME/modules/imframework/fileStagingArea/elementData"/>
        </converter>
    </convertItem>
</emsElement>
<emsElement>
    <name>n-nj</name>
    <frameworkFrequencyMultiplier>1</frameworkFrequencyMultiplier>
    <convertItem>
        <source user="ubuntu" url="sftp://192.168.150.133/home/ubuntu/NSN/NJ" passwd="d5ee45fa2386d5f8">
            <additional>strictHostKeyChecking=no</additional>
            <namePatterm>**/*</namePatterm>
            <recureMode>true</recureMode>
            <useEncryptPasswd>true</useEncryptPasswd>
        </source>
        <processor deltaed="true" name="nsn">
            <serverName>192.168.150.133</serverName>
            <config>configA=$NH_HOME/custom/imframework/config/NSN-SimulatorConfigA.xml</config>
        </processor>
        <converter name="dump">
            <location url="$NH_HOME/modules/imframework/fileStagingArea/dump"/>
        </converter>
        <converter name="element">
            <location url="$NH_HOME/modules/imframework/fileStagingArea/elementData"/>
        </converter>
    </convertItem>
</emsElement>
<emsElement>
    <name>n-tx</name>
    <frameworkFrequencyMultiplier>1</frameworkFrequencyMultiplier>
    <convertItem>
        <source user="ubuntu" url="sftp://192.168.150.133/home/ubuntu/NSN/TX" passwd="d5ee45fa2386d5f8">
            <additional>strictHostKeyChecking=no</additional>
            <namePatterm>**/*</namePatterm>
            <recureMode>true</recureMode>
            <useEncryptPasswd>true</useEncryptPasswd>
        </source>
        <processor deltaed="true" name="nsn">
            <serverName>192.168.150.133</serverName>
            <config>configA=$NH_HOME/custom/imframework/config/NSN-SimulatorConfigA.xml</config>
        </processor>
        <converter name="dump">
            <location url="$NH_HOME/modules/imframework/fileStagingArea/dump"/>
        </converter>
        <converter name="element">
            <location url="$NH_HOME/modules/imframework/fileStagingArea/elementData"/>
        </converter>
        <converter name="csv">
            <additional>perDayperDirectory=true,PDSN=starent</additional>
        </converter>
    </convertItem>
</emsElement>
<emsElement>
    <name>k1</name>
    <frameworkFrequencyMultiplier>1</frameworkFrequencyMultiplier>
    <convertItem>
        <source user="ubuntu" url="sftp://192.168.150.128/home/ubuntu/fileStagingArea" passwd="d5ee45fa2386d5f8">
            <additional>strictHostKeyChecking=no</additional>
            <namePatterm>**/*</namePatterm>
            <recureMode>true</recureMode>
            <useEncryptPasswd>true</useEncryptPasswd>
        </source>
        <processor deltaed="true" name="kodiak" additional="processBy=KodiakFileDate">
            <serverName>192.168</serverName>
            <config>$NH_HOME/custom/imframework/config/KODIAK-SimulatorConfig.xml</config>
        </processor>
        <converter name="dump">
            <location url="$NH_HOME/modules/imframework/fileStagingArea/dump"/>
        </converter>
        <converter name="element">
            <location url="$NH_HOME/modules/imframework/fileStagingArea/elementData"/>
        </converter>
    </convertItem>
</emsElement>
<emsElement>
    <name>a1</name>
    <frameworkFrequencyMultiplier>1</frameworkFrequencyMultiplier>
    <convertItem>
        <source user="ubuntu" url="sftp://192.168.150.133/home/ubuntu/ALU/NJALUSample" passwd="d5ee45fa2386d5f8">
            <additional>strictHostKeyChecking=no</additional>
            <namePatterm>**/*</namePatterm>
            <recureMode>true</recureMode>
            <useEncryptPasswd>true</useEncryptPasswd>
        </source>
        <processor deltaed="true" name="alu">
            <serverName>192.168.150.133</serverName>
            <config>configA=$NH_HOME/custom/imframework/config/ALU-SimulatorConfigA.xml,configB=$NH_HOME/custom/imframework/config/ALU-SimulatorConfigB.xml,configC=$NH_HOME/custom/imframework/config/ALU-SimulatorConfigC.xml</config>
        </processor>
        <converter name="dump">
            <location url="$NH_HOME/modules/imframework/fileStagingArea/dump"/>
        </converter>
        <converter name="element">
            <location url="$NH_HOME/modules/imframework/fileStagingArea/elementData"/>
        </converter>
    </convertItem>
</emsElement></module>

【问题讨论】:

您能否发布您遇到的错误以及您遇到问题的代码的 sn-p? 我的代码没有任何错误。数据正在加载到网格中。但是xml文档的内部子节点数据没有被加载。 我想我只是不明白这个问题。您无法访问 JAXB 生成的类中的 getter,还是未编组的 JAXB 对象树中没有数据? 我还没有在实际代码中使用 JAXB。我想先用最简单的代码加载并查看网格中的 XML 文档。 【参考方案1】:

您将编写的 JAXB 代码如下所示:

JAXBContext jc = JAXBContext.newInstance("your.model.package");

Unmarshaller unmarshaller = jc.createUnmarshaller();
Module module = (Module) unmarshaller.unmarshal(xml);

【讨论】:

以上是关于带有 JAXB 类的 GXT的主要内容,如果未能解决你的问题,请参考以下文章

从 xsd 生成 c++ 类的 JAXB 等效项?

带有元素引用的 JAXB 多个模式

如何拥有两个具有不同命名空间和相同 JAXB 类的不同端点?

Unmarshal NullPointerException(带有 JAXB 参考实现)

泛型类的 JAXB 序列化失败

如何指定用于 JAXB xjc 生成的类的 XmlAccessorType