举个例子,假设我有一个已使用 JAXB(或 XStream)解码的 customerX.xml 文件。
我在我的 Java 对象中更新了这个客户的电话号码,然后将内容编码回同一个 customerX.xml
Here's 一个 xml 更新的旧 jaxb 示例,从 2003 年开始……不过可能有更好的方法。
问题
编码是否会写入文件的所有内容,尽管我只是更新单个元素?如果是,那么是否有更好的方法来使用 JAXB 或 XStream 编码和更新 XML 文件的特定元素?
如何使编码操作线程安全,以防多个线程可能编码到同一个 XML 文件?
最佳答案
Will marshaling write all contents of the file, though I'm just updating a single element?
是的。编码器不知道也不关心你改变了多少,它只关心对象在编码时的状态。
is there a better way to marshal and update a specific element of an XML file using JAXB or XStream?
不是真的。如果您需要更新数据片段,那么数据确实应该存储为单独的文件,而不是一个大文件。
How do I make a marshalling operation thread safe, in case multiple threads might marshal to the same XML file?
使用常规的 java 线程同步技术(即使用 synchronized
方法,或使用 java.util.concurrent
中的锁定工具)。
关于java - 如何编码使用 JAXB(或 XStream)更新 XML 文件而不写入整个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6783202/