简体   繁体   English

StAX如何处理XML修改

[英]How does StAX handle XML modifications

Modifying XML files with StAX is possible. 可以使用StAX修改XML文件。 But what I am trying to understand is - 但我想了解的是-
--> is it possible with StAX to update the XML documents as and when it encounters an event. -> StAX是否有可能在遇到事件时更新XML文档。 If it does so, then there is no huge memory footprint which is great . 如果这样做,那么就不会有很大的内存占用。
Example - if i am reading a Customers.xml file and I need to change the State information for each customer from StateName to StateCode. 示例 -如果我正在读取Customer.xml文件,并且需要将每个客户的州信息从StateName更改为StateCode。 Then When I encounter the content - 然后,当我遇到内容时-
<State>California</State> I want to change it to <State>CA</State> <State>California</State>我想将其更改为<State>CA</State>

So with StAX does can this modification to the source file happen immediately after reading <State>California</State> and only only after that the parser goes ahead with the next customer record. 因此,使用StAX可以在读取<State>California</State>之后立即对源文件进行这种修改,并且只有在解析器继续进行下一个客户记录之后才进行。 So when the second customers record is read the first customers state is already updated in the xml to state code. 因此,当读取第二个客户记录时,第一个客户状态已在xml中更新为状态代码。

or 要么
--> does it handle updates by temporarily keeping track of the changes to be made and updates the whole document in a single go after parsing the entire document. ->是否通过临时跟踪要进行的更改来处理更新,并在解析整个文档后一次更新整个文档。 In this case i will guess there will be huge memory footprint if there are too many changes to larges documents (say 10GB XML file). 在这种情况下,我想如果大型文档(例如10GB XML文件)的更改过多,将会占用大量内存。
Example continued - so when second customer is processed StAX knows that state field for the first customer needs to be updated but it defers it until all the customers records are read. 示例继续进行 -因此,在处理第二个客户时,StAX知道第一个客户的状态字段需要更新,但是它推迟了该字段,直到读取所有客户记录为止。 It can use some in-memory mechanism to keep track of what needs to be updated in to XML. 它可以使用某种内存中的机制来跟踪需要更新为XML的内容。

You cannot change XML files in-place with StAX, but you can read the file in, write to another file and apply changes on the fly. 您不能使用StAX就地更改XML文件,但是可以读入文件,写入另一个文件并即时应用更改。 The modified StAX events (including the changes) are written immediately to the target file (except for internal buffering purposes). 修改后的StAX事件(包括更改)将立即写入目标文件(内部缓冲目的除外)。

So the size of your XML file or the number of changes doesn't matter. 因此,您的XML文件的大小或更改的数量无关紧要。

If your changes depend on other parts in the XML then it becomes more difficult. 如果您的更改依赖于XML中的其他部分,那么它将变得更加困难。 Then you can process the XML file in two passes. 然后,您可以分两步处理XML文件。 Pass 1 is for collecting all necessary information for the changes and pass 2 is for applying the changes with the information gathered in pass 1. Or you can use a totally different approach like XML databases (eg BaseX) and apply your changes with XQuery. 通道1用于收集所有必要的更改信息,而通道2用于将更改与通道1中收集的信息一起应用。或者,您可以使用完全不同的方法(例如XML数据库(例如BaseX)),并使用XQuery来应用更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM