Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7700201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:39:13+00:00 2026-05-31T22:39:13+00:00

I would like to update an xml file from another xml file.I have used

  • 0

I would like to update an xml file from another xml file.I have used an xml file as shown below:

one.xml

    <?xml version="1.0" encoding="utf-8"?>
   <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="#00BFFF">   
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:gravity="center" android:visibility="visible">
</LinearLayout>
<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:gravity="center" android:visibility="visible">

</LinearLayout>

</LinearLayout>
</ScrollView>

two.xml as follows:

      <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
       <map>
      <int name="linearLayout1" value="8" />
      <int name="linearLayout2" value="0" />
      </map>

from the above two xml files i would like to change the attribute value one. xml when
if

  <int name ="linearLayout1" value = "8"/> 

from two.xml then I would like to update one.xml file as where LinearLayout android:id=”@+id/linearLayout1″ then change the attribute value as android:visibility=”gone”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:39:14+00:00Added an answer on May 31, 2026 at 10:39 pm

    Here is code what you want it’s

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse("/home/riddhish/developerworkspace/SplitString/src/com/updatexmlwithjava/two.xml");           
        DocumentTraversal traversal = (DocumentTraversal) doc;
        Node a = doc.getDocumentElement();
        NodeIterator iterator = traversal.createNodeIterator(a, NodeFilter.SHOW_ELEMENT, null, true);
    

    /**
    * Logic for checking
    **/

    boolean flag=false;
    for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {
           Element e = (Element) n;                
                if ("int".equals(e.getTagName())) {
                    if(e.getAttribute("name").equals("linearLayout1")){
                            if(e.getAttribute("value").equals("8"))
                                flag=true;                      
                        }                    
                } 
    }
    

    /**
    * Logic for reading one.xml and setting android:visibility=”gone”
    **/

    docFactory = DocumentBuilderFactory.newInstance();
    docBuilder = docFactory.newDocumentBuilder();
    doc = docBuilder.parse("/home/riddhish/developerworkspace/SplitString/src/com/updatexmlwithjava/one.xml");           
    traversal = (DocumentTraversal) doc;
    a = doc.getDocumentElement();
    iterator = traversal.createNodeIterator(a, NodeFilter.SHOW_ELEMENT, null, true);            
    for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {
          Element e = (Element) n;                
              if ("LinearLayout".equals(e.getTagName())) {
                    if(e.getAttribute("android:id").equals("@+id/linearLayout1")){
                            if(flag==true){
                                System.out.println(""+e.getAttribute("android:visibility"));
                                e.setAttribute("android:visibility", "gone");
                            }                           
                    }                    
              } 
    }
    

    /**
    * Logic for rewriting one.xml
    **/

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File("/home/riddhish/developerworkspace/SplitString/src/com/updatexmlwithjava/one.xml"));
    iterator = traversal.createNodeIterator(a, NodeFilter.SHOW_ELEMENT, null, true);          
    doc = docBuilder.newDocument();
    Element rootElement = doc.createElement("ScrollView");
    doc.appendChild(rootElement);
    for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {               
            rootElement.appendChild(doc.importNode(n, true));
    }
    transformer.transform(source, result);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML document that I would like to update after it already
I have XML file which looks like this: <?xml version=1.0?> <results> <row id=100><name>name blah</name></row>
I have a list of custom objects List and I would like to update
_stuckVertices is an array of pointers and I would like to update one index
I have a couple of tables (mySQL) that i would like to update with
I've a collection of a class' properties and would like to update each one's
I have a table in MS SQL 2005. And would like to do: update
I would like to add an update hook to git that prevents people from
I would like to add an update hook to git that prevents people from
I would like to have something like this be generated from hbm2ddl: ______________ ______________

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.