in Education by
I have to Update the Xml document object generated using Apache XMLbeans.There are two ways I am trying to update and save the document. Step 1 : I am parsing the document and updating with the new values and saving with the parsed document itself. private boolean updateContact(ContactType contacts, String contactFilePath, String name) throws Exception { ContactsDocument contactDoc = ContactsDocument.Factory.parse(new File(contactFilePath)); ContactType contact = contactDoc.getContactType(); contact.setName(name); contactDoc.save(new File(contactFilePath) , XmlUtils.getDefaultFileSavingOptions()); } Step 2 : I am passing the updated document type and creating new instance of the xml document and saving with the updated type. private boolean writeContact(ContactType contactType, String contactFilePath) throws Exception { ContactsDocument contactsDoc = ContactsDocument.Factory.newInstance(); contactsDoc.setContactType(contactType); contactsDoc.save(new File(contactFilePath), XmlUtils.getDefaultFileSavingOptions()); } The step 2 is working but i want to know, will step 1 work ? and which is the efficient way of doing it for this scenario. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The Step 1 works perfect with the XML default file saving options and it does not modifies or removes any of the existing namespaces present in the file. private boolean updateContact(ContactType contacts, String contactFilePath, String name) throws Exception { ContactsDocument contactDoc = ContactsDocument.Factory.parse(new File(contactFilePath)); ContactType contact = contactDoc.getContactType(); contact.setName(name); contactDoc.save(new File(contactFilePath) , XmlUtils.getDefaultFileSavingOptions()); } It is also good approach to parse the file and save the changes on top of it, Instead of parse and instantiating the xml document for every updates.

Related questions

0 votes
    Rather than rewriting the entire contents of an xml file when a single element is updated, is there a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 26, 2022 in Education by JackTerrance
0 votes
    Which of the following is not an Enterprise Beans type? (a) Doubleton (b) Singleton (c) Stateful (d) Stateless ... & JDBC of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which of the following is not true about Java beans? (a) Implements java.io.Serializable interface (b) Extends java. ... & JDBC of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Java Beans are extremely secured? (a) True (b) False I got this question in unit test. Question is taken from ... Beans & JDBC of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which of the following is not a feature of Beans? (a) Introspection (b) Events (c) Persistence (d) Serialization ... & JDBC of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    In my application when the current logged in user's usernam is changed i want to update the UserDetails ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
0 votes
    Which of these methods calls update() method? (a) notify() (b) observeObject() (c) updateObserver() (d) ... Utility Classes of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Ctrl + n in MS word is used to- (a) Save document (b) Start Paragraph (c) Open new document (d) Close ... (e) Open a saved document Select the correct answer from above options...
asked Dec 10, 2021 in Education by JackTerrance
0 votes
    You are working with this XML code snippet from the XML document cars.xml. You need to return the information about the cars ... D.doc("cars.xml")/cars/car[integer(year) > 2000]...
asked Mar 10, 2023 in Technology by JackTerrance
0 votes
    The syntax for retrieving specific elements from an XML document is ______________. (1)XMLSchema (2)XQuery (3)XPath...
asked Apr 23, 2021 in Technology by JackTerrance
0 votes
    The syntax for retrieving specific elements from an XML document is __________. (1)XML Schema (2)XQuery (3)XPath...
asked Apr 22, 2021 in Technology by JackTerrance
0 votes
    Which of these object stores association between keys and values? (a) Hash table (b) Map (c) Array (d ... Collections Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these type parameters is used for a generic class to return and accept any type of object? (a) K ... & Packages of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which of these type parameters is used for a generic methods to return and accept any type of object? (a) ... chapter Generics of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which object stores references to the request and response objects? (a) sessionContext (b) pageContext (c) HttpSession ... API of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
...