java 在代码中使用绑定类 - JIBX

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10476020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:13:33  来源:igfitidea点击:

Using binded classes in your code - JIBX

javaxmlxsdjibx

提问by Rajeshwar

I managed to bind the files generated through an XSD successfully and i got the following files in my dir

我设法成功绑定了通过 XSD 生成的文件,并且在我的目录中获得了以下文件

C:\jibx\tutorial\example25>dir
Volume in drive C is WINDOWS
Volume Serial Number is 1C8F-663E

Directory of C:\jibx\tutorial\example25

05/06/2012  07:12 PM    <DIR>          .
05/06/2012  07:12 PM    <DIR>          ..
05/06/2012  07:12 PM             3,313 Address.class
05/06/2012  07:08 PM             3,447 Address.java
05/06/2012  07:10 PM             2,912 binding.xml
05/06/2012  07:12 PM             2,516 Customer.class
05/06/2012  07:08 PM             1,763 Customer.java
05/06/2012  07:12 PM             2,582 Item.class
05/06/2012  07:08 PM             1,878 Item.java
05/06/2012  07:12 PM             2,529 JiBX_bindingFactory.class
05/06/2012  07:12 PM             2,384 JiBX_bindingMungeAdapter.class
05/06/2012  07:12 PM             2,490 JiBX_bindingOrder_access.class
05/06/2012  07:12 PM             7,539 Order.class
05/06/2012  07:09 PM             4,869 Order.java
05/06/2012  07:11 PM             1,034 Shipping.class
05/06/2012  07:09 PM               879 Shipping.java
05/06/2012  12:22 AM             5,137 starter.xsd
          15 File(s)         45,272 bytes
           2 Dir(s)  160,023,375,872 bytes free

Now i copied these files into a newly created JAVA project in eclipse , created a package called example25 in that project and pasted those files there I then used the following code and ran it in the debug mode

现在我将这些文件复制到 eclipse 中新创建的 JAVA 项目中,在该项目中创建了一个名为 example25 的包并将这些文件粘贴到那里,然后我使用以下代码并在调试模式下运行它

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Iterator;

import org.jibx.runtime.BindingDirectory;
import org.jibx.runtime.IBindingFactory;
import org.jibx.runtime.IMarshallingContext;
import org.jibx.runtime.IUnmarshallingContext;
import org.jibx.runtime.JiBXException;
import example25.*;


public class Test
{

public static void main(String[] args)
{

        try
        {

            // unmarshal customer information from file
            IBindingFactory bfact = BindingDirectory.getFactory(Order.class);
            IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
            FileInputStream in = new FileInputStream("D:\Java Libraries\jibx\dwcode2\starter.xml");
            Order order = (Order)uctx.unmarshalDocument(in, null);

            // compute the total amount of the order
            float total = 0.0f;
            for (Iterator<Item> iter = order.getItemList().iterator(); iter.hasNext();)
            {
                Item item = iter.next();
                total += item.getPrice() * item.getQuantity();
            }
            order.setTotal(new Float(total));

            // marshal object back out to file (with nice indentation, as UTF-8)
            IMarshallingContext mctx = bfact.createMarshallingContext();
            mctx.setIndent(2);
            FileOutputStream out = new FileOutputStream("c:\out.xml");
            mctx.setOutput(out, null);
            mctx.marshalDocument(order);
            System.out.println("Processed order with " +  order.getItemList().size() + " items and total value " + total);

        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
            System.exit(1);
        } catch (JiBXException e)
        {
            e.printStackTrace();
            System.exit(1);
        }
    }

}//end main

I get the following error after the execution of the first statement

执行第一条语句后出现以下错误

Unable to access binding information for class example25.Order
Make sure the binding has been compiled
java.lang.NoSuchFieldException: JiBX_bindingList
at java.lang.Class.getDeclaredField(Unknown Source)
at org.jibx.runtime.BindingDirectory.getBindingList(BindingDirectory.java:68)
at org.jibx.runtime.BindingDirectory.getFactory(BindingDirectory.java:211)
at Test.main(Test.java:24)

回答by Don Corley

Rajesh,

拉杰什,

The files that you show in that directory mean you did everything correctly!

您在该目录中显示的文件意味着您所做的一切都是正确的!

The mistake you made was to use eclipse, since eclipse automatically compiles the source code files again (without the binding information you added in the binding step)

你犯的错误是使用eclipse,因为eclipse会自动重新编译源代码文件(没有你在绑定步骤中添加的绑定信息)

Try running the Test.class file with the class files you generated in the first step.

尝试使用您在第一步中生成的类文件运行 Test.class 文件。

Your java command should be something like this:

你的 java 命令应该是这样的:

java -cp C:\directorywithtestclass;C:\jibx\tutorial Test

Don

大学教师

回答by Divs

Since JiBX Binding compiler modifies generated class files and puts binding information using the binding.xml, so we need to use Binding compilerto generate the class files from the generated .java files and binding.xml. To use eclipse to do this we can use as explained in this link.

由于JiBX Binding 编译器修改生成的类文件并使用binding.xml 放置绑定信息,因此我们需要使用Binding 编译器从生成的.java 文件和binding.xml 生成类文件。要使用 eclipse 来做到这一点,我们可以按照此链接中的说明使用。