gpt4 book ai didi

java - 使用 JAXB 切割 XML

转载 作者:行者123 更新时间:2023-11-30 09:35:47 27 4
gpt4 key购买 nike

我有以下 xml:



<Package>
<PackageHeader>
<name>External Vendor File</name>
<description>External vendor file for some purpose</description>
<version>3.141694baR3</version>
</PackageHeader>
<PackageBody>
<Characteristic id="1">
<Size>
<value>1.68</value>
<scale>Meters</scale>
<comment>Size can vary, depending on temperature</comment>
</Size>
<Weight>
<value>9</value>
<scale>M*Tons</scale>
<comment>His mama is so fat, we had to use another scale</comment>
</Weight>
<rating>
<ratingCompany>ISO</ratingCompany>
<rating:details xmlns:rating="http://www.w3schools.com/ratingDetails">
<rating:value companyDepartment="Finance">A</rating:value>
<rating:expirationDate update="1/12/2010">1/1/2014</rating:expirationDate>
<rating:comment userID="z94234">You're not Silvia.</rating:comment>
<rating:comment userID="r24942">You're one of the Kung-Fu Creatures On The Rampage</rating:comment>
<rating:comment userID="i77880">TWO!</rating:comment>
<rating:priority>3</rating:priority>
</rating:details>
</rating>
</Characteristic>
<Characteristic id="2">
<Size/>
<Weight/>
<rating/>
</Characteristic>
...
<Characteristic id="n"/>
</PackageBody>
</Package>

以及以下 Java 代码:



public class XMLTest {

public static void main(String[] args) throws Exception {

Package currentPackage = new Package();
Package sourcePackage = new Package();
int totalCharacteristics;

PackageBody currentPackageBody = new PackageBody();
Characteristic currentCharacteristic = new Characteristic();
rating currentRating = new rating();

FileInputStream fis = new FileInputStream("sourceFile.xml");

JAXBContext myCurrentContext = JAXBContext.newInstance(Package.class);
Marshaller m = myCurrentContext.createMarshaller();
Unmarshaller um = myCurrentContext.createUnmarshaller();

sourcePackage = (Package)um.unmarshal(fis);

currentPackage.setPackageHeader(sourcePackage.getPackageHeader());

totalCharacteristics = sourcePackage.getPackageBody().getCharacteristics().size();
for (int i = 0; i < totalCharacteristics; i++)
{
currentRating = sourcePackage.getPackageBody().getCharacteristics().get(i).getrating();
}

currentCharacteristic.setrating(currentRating);
currentPackageBody.getCharacteristics().add(currentCharacteristic);

currentPackage.setPackageBody(currentPackageBody);
m.marshal(currentPackage, new File("targetFile.xml"));
fis.close();
}
}

这给了我下一个 XML:



<Package>
<PackageHeader>
<name>External Vendor File</name>
<description>External vendor file for some purpose</description>
<version>3.141694baR3</version>
</PackageHeader>
<PackageBody>
<Characteristic id="1">
<rating>
<ratingCompany>ISO</ratingCompany>
<rating:details xmlns:rating="http://www.w3schools.com/ratingDetails">
<rating:value companyDepartment="Finance">A</rating:value>
<rating:expirationDate update="1/12/2010">1/1/2014</rating:expirationDate>
<rating:comment userID="z94234">You're not Silvia.</rating:comment>
<rating:comment userID="r24942">You're one of the Kung-Fu Creatures On The Rampage</rating:comment>
<rating:comment userID="i77880">TWO!</rating:comment>
<rating:priority>3</rating:priority>
</rating:details>
</rating>
</Characteristic>
<Characteristic id="2">
<rating/>
</Characteristic>
...
<Characteristic id="n"/>
</PackageBody>
</Package>

这就是我需要的:



<Package>
<PackageHeader>
<name>External Vendor File</name>
<description>External vendor file for some purpose</description>
<version>3.141694baR3</version>
</PackageHeader>
<PackageBody>
<Characteristic>
<rating id="1">
<ratingCompany>ISO</ratingCompany>
<rating:details xmlns:rating="http://www.w3schools.com/ratingDetails">
<rating:comment userID="z94234">You're not Silvia.</rating:comment>
<rating:comment userID="r24942">You're one of the Kung-Fu Creatures On The Rampage</rating:comment>
<rating:comment userID="i77880">TWO!</rating:comment>
<rating:priority>3</rating:priority>
</rating:details>
</rating>
</Characteristic>
<Characteristic>
<rating id="2"/>
</Characteristic>
...
<Characteristic/>
</PackageBody>
</Package>

但是我有几个问题:

  • 我如何实现读取 4GB 文件的方法? (例如,使用 StAX 阅读)。

  • 如果我想从源到目标过滤一些标签(如在最后一个 xml 中),我是否必须将它们一个一个分配给目标文件?是否有任何迭代器可以让我遍历所有子节点并分配它们?

  • 如果源文件发生变化,我是否需要重新运行 xjc 并重新编译整个项目?

谢谢。

最佳答案

要读取巨大的 XML 文件,您肯定需要像 StAX 这样的流式解析器。此外,您可以使用 JAXB 的组合来选择性地将给定的 xml 片段映射到 java 对象(如果您希望使用它)。仅当模式更改时才需要重新生成 JAXB 类。如果您的应用程序代码发生更改,则无需重新生成。

关于java - 使用 JAXB 切割 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11158079/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com