当前位置: 首页 > 知识库问答 >
问题:

循环一个jaxb marshall

百里弘致
2023-03-14

我只想循环一个具有相同xml元素但具有不同ID的jaxb封送,这与下面的代码不同,我正在为每个ID解析新的xml文件,并重写旧的xml文件。

数据[][]是从矩阵中读取的,数据[i][0]代表一个ID列表,我想将这些ID设置为客户ID。

      Customer customer = new Customer();
      File file = new File("C:/data.xml");

      for (int i = 0; i < data.length; i++) {
          customer.setId(data[i][0]);

            JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();


            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            jaxbMarshaller.marshal(customer, file);
            jaxbMarshaller.marshal(customer, System.out);


      }

以上代码的输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="1"/>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="2"/>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="3"/>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="4"/>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="5"/>

我想把所有ID输出到一个xml文件中,有什么提示吗?

共有2个答案

匡祖鹤
2023-03-14

只需将marhsaller的构造移出循环。您可以在同一线程中多次重复使用编组器。

或者您真的试图将客户列表作为单个xml文件输出吗?然后你需要一个客户对象数组,然后整理这个数组。

国兴贤
2023-03-14

您可以在Marshaller上设置JAXB_FRAGMENT属性,以防止写入标头。编组到另一个文档时,这是必要的。

package forum12925616;

import java.io.*;
import javax.xml.bind.*;

public class Demo {

    private static int X = 5;
    private static int Y = 2;

    public static void main(String[] args) throws Exception {
        int[][] data = new int[X][Y];
        for (int x = 0; x < X; x++) {
            for (int y = 0; y < Y; y++) {
                data[x][y] = x;
            }
        }

        // Create this once
        JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

        marshal(jaxbMarshaller, data, System.out);

        FileOutputStream fileOutputStream = new FileOutputStream("src/forum12925616/out.xml");
        marshal(jaxbMarshaller, data, fileOutputStream);
        fileOutputStream.close();
    }

    private static void marshal(Marshaller jaxbMarshaller, int[][] data, OutputStream outputStream) throws Exception {
        OutputStreamWriter writer = new OutputStreamWriter(outputStream);
        writer.write("<?xml version='1.0' encoding='UTF-8' standalone='yes'?>");
        writer.write("\n<customers>\n");
        for (int i = 0; i < data.length; i++) {
            Customer customer = new Customer();
            customer.setId(data[i][0]);
            jaxbMarshaller.marshal(customer, writer);
            writer.write("\n");
        }
        writer.write("<customers>");
        writer.flush();
    }

}

系统和文件输出

下面是控制台和文件的结果输出。

<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<customers>
<customer id="0"/>
<customer id="1"/>
<customer id="2"/>
<customer id="3"/>
<customer id="4"/>
<customers>

获取更多信息

  • http://blog.bdoughan.com/2011/08/jaxb-and-java-io-files-streams-readers.html
 类似资料:
  • 我的程序中有两个while循环。第一个是针对游戏菜单的,第二个是针对实际游戏的。如果“Gameover-Event”发生,我想返回菜单。我不知道该怎么做。

  • 问题在代码的注释中,很抱歉,我认为它更整洁,因为流程很重要,我想。。。 //*这是来自Oracle:(https://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html#hasNextInt()) ”hasNextInt 公共布尔值hasnetint() 如果此扫描仪输入中的下一个标记可以使用nextInt()方法解释为默认基数中的in

  • 我需要12次外循环,每个外循环1次内循环 例如: 等 但现在我的内循环在第一次迭代的外循环上运行。 请帮助我如何使用for循环执行此操作。

  • 问题内容: 我有以下代码: 这里的主要循环是: 但是我不确定这是做到这一点的最佳方法(如果我想输入一些信息,这将不起作用) 然后我尝试了这个: 但是,正如我已经意识到的那样,它并没有达到我的预期。所以问题是:创建主循环的最佳方法是什么? 问题答案: Tkinter为此提供了一个强大的工具,它被称为after。它旨在用作同步睡眠命令,但可以通过调用自身在mainloop内建立一个循环。 之后,是一个

  • 我将获取一个用户列表作为数组,对数据进行分页,并以表格的形式显示在视图中。 为了迭代数组,我使用 foreach 循环。但是我的前循环迭代不起作用。 这是一个示例数组,当我执行< code>print_r()时,我有< code>id、< code>email和< code>full_name字段,我想在视图中显示它们 这是我迭代数组的视图部分。结果存储在< code>$userList中。这个数

  • 我在汇编语言中遇到了循环问题。 当我们想要使用计数器寄存器在嵌套循环中循环时,我们首先要做的是将计数器寄存器的值移动到外部循环的堆栈中,然后在我们完成内部循环时取回它,这样我们就能够使用一个计数器寄存器在每个循环中循环不同的迭代次数。 但是嵌套循环中的嵌套循环呢? 我想打印一个由字符< code>S组成的金字塔。我得到的是, 我真正想要的是, 这是我的程序代码 为了实现我想要的,我需要在嵌套循环中