简体   繁体   中英

Traverse (Read) static DOM document object is thread-safe or not?

I created a DOM document static object, such as below, it uses javax.xml.parsers.* and org.w3c.dom.* API:

        DocumentBuilderFactory docBldrFactry = DocumentBuilderFactory.newInstance();
        docBldrObj = docBldrFactry.newDocumentBuilder();

        File file = new File(fileDirectory);
        // Parse the XML file and return a DOM document object

        document = docBldrObj.parse(file);
        //FYI, document is declared as private static org.w3c.dom.Document document elsewhere. 

Later after created above, If this static DOM document object shared by threads, but all threads are just read (traverse) this document, is it thread safe?

I assume it is since read should not modify this shared state, but not sure whether internally there is some magic about it which I don't know.

Thanks

The problem solved by writing own simple Document structure. Eg, clone the DOM document into that, which is thread-safe on read operations. FYI, for my own purpose, when cloning the document, I don't clone everything but the information based on my need (COMMENT_NODE, TEXT_NODE, ELEMENT_NODE, attributes).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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