2

我有以下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 中),我是否必须将它们一一分配给 targetFile?是否有任何迭代器可以让我遍历所有子节点并分配它们?

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

谢谢。

4

1 回答 1

2

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

于 2012-06-22T16:39:21.757 回答