是否可以使用JAXB(或JAXB + StAX)编组XML的一部分? [英] Can a part of XML be marshalled using JAXB (or JAXB + StAX)?

查看:131
本文介绍了是否可以使用JAXB(或JAXB + StAX)编组XML的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常庞大的XML结构。
我想通过解组一个元素然后应用业务逻辑来更新这个XML的部分。

I have an XML structure which is very huge. I want to update the parts of this XML, by unmarshalling one element and then applying business logic.

我能够将子元素解组成POJO 。我想在Java中对这个POJO进行更改,然后在同一位置将其更新回XML。

I am able to unmarshal a child element into a POJO. I want to make changes to this POJO in Java and then update it back to the XML at the same location.

这在JAXB中是否可行?或者使用JAXB + StAX的组合。

Is this possible in JAXB? Or by using the combination of JAXB + StAX.

示例结构:

 <folder id="c5718b36-bab1-4c08-8f75-8e2f9aee42c5" name="Folder-1">
        <description> folder Desc</description>
        <createdBy>User2</createdBy>
        <hidden>false</hidden>

        <file id="4f2efb42-0604-4878-9e1e-ae90d66fb836" name="File-1">
            <description>file desc</description>
            <createdBy>User1</createdBy>
            <hidden>false</hidden>
        </file>
 </folder>

在上面的例子中,我能够将'file'元素解组为POJO。我想对此POJO进行更改,然后在XML文件中的正确位置更新它。

In the above example, I am able to unmarshal a 'file' element into a POJO. I want to make changes to this POJO and then update the same in the XML file at its correct location.

如何完成此操作?

请帮帮我。
谢谢。

Please help me. Thanks.

推荐答案

您可以使用JAXB和StAX执行以下操作:

You could do the following with JAXB and StAX:

import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.stream.events.XMLEvent;
import java.io.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        XMLInputFactory xif = XMLInputFactory.newFactory();
        XMLOutputFactory xof = XMLOutputFactory.newFactory();

        try(
           FileInputStream in = new FileInputStream("in.xml");
           FileOutputStream out = new FileOutputStream("out.xml");
        ) {
            XMLEventReader xer = xif.createXMLEventReader(in);
            XMLEventWriter xew = xof.createXMLEventWriter(out);

            JAXBContext jc = JAXBContext.newInstance(File.class);
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

            while(xer.hasNext()) {
                if(xer.peek().isStartElement() && xer.peek().asStartElement().getName().getLocalPart().equals("file")) {
                    // Unmarshal the File object from the XMLEventReader
                    File file = (File) unmarshaller.unmarshal(xer);

                    // Modify the File object
                    file.description = "NEW DESCRIPTION";

                    // Marshal the File object to the XMLEventWriter
                    marshaller.marshal(file, xew);
                } else {
                    // Copy node from reader to writer
                    xew.add(xer.nextEvent());
                }
            }
        }
    }

}

这篇关于是否可以使用JAXB(或JAXB + StAX)编组XML的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆