有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java循环a jaxb

我只想循环一个具有相同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"/>

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


共 (2) 个答案

  1. # 1 楼答案

    您可以在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>
    

    了解更多信息

  2. # 2 楼答案

    只需将marhsaller的构造移出循环即可。您可以在同一线程中重复使用封送拆收器

    或者您真的试图将客户列表作为单个xml文件输出吗?然后需要一个客户对象数组,然后对该数组进行马歇尔处理