Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用JiBX读取XML文档?_Java_Xml_Jibx - Fatal编程技术网

Java 如何使用JiBX读取XML文档?

Java 如何使用JiBX读取XML文档?,java,xml,jibx,Java,Xml,Jibx,我试着做示例代码 public class xml_class { /** * Unmarshal the sample document from a file, compute and set order total, then * marshal it back out to another file. * * @param args */ public static void main(String[] args) {

我试着做示例代码

public class xml_class {
    /**
     * Unmarshal the sample document from a file, compute and set order total, then
     * marshal it back out to another file.
     *
     * @param args
     */
    public static void main(String[] args) {
        try {

            // unmarshal customer information from file
            IBindingFactory bfact = BindingDirectory.getFactory(test.Order.class);
            IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
            FileInputStream in = new FileInputStream("D:/liferay-develop/data1.xml");
            Order order = (Order)uctx.unmarshalDocument(in, null);

            // compute the total amount of the order
            float total = 0.0f;
            for (Iterator<Item> iter = order.getItems().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("D:/liferay-develop/out.xml");
            mctx.setOutput(out, null);
            mctx.marshalDocument(order);
            System.out.println("Processed order with " + order.getItems().size() +
                " items and total value " + total);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        } catch (JiBXException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
}
什么是绑定?如何使用它?

Eli, 你为什么不从一个简单的例子开始呢。 下面是一个很好的开始: 检查并构建它,然后在org.jibx.schema.test.person项目中运行简单的编组/解编组示例。 我希望这有帮助。
Don

绑定定义XML结构如何与Java对象相对应。我认为这是一个很好的开始。我看了他们,但是,我的英语很糟糕,读这么多课文很难。我尝试使用EclipseJiBX插件。但一切都没有改变。
Unable to access binding information for class test.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.xml_class.main(xml_class.java:31)