how to update xml file from another xml file dynamically?

前端 未结 1 1939
自闭症患者
自闭症患者 2020-11-28 15:55

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

one.xml

    

        
相关标签:
1条回答
  • 2020-11-28 16:45

    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 讨论(0)
提交回复
热议问题