2011-04-03 97 views
1

所以我試圖在我的android應用程序中創建一個xml文檔。我正在使用編寫Java應用程序時使用的代碼。我試圖如下所示:在android中創建一個xml文檔

public void createxml() throws SAXException, IOException { 
     try { 

      File file = new File(Environment.getExternalStorageDirectory() 
        + File.separator + "images" + File.separator + "newxml.xml"); 
      DocumentBuilderFactory docFactory = DocumentBuilderFactory 
        .newInstance(); 
      DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); 
      Document doc = docBuilder.parse(file); 

      // root elements 
      // Document doc = docBuilder.newDocument(); 

      // Element rootElement = doc.get 
      // doc.appendChild(rootElement); 
      Node node = doc.getDocumentElement(); 

      // staff elements 
      Element outfit = doc.createElement("outfit"); 
      node.appendChild(outfit); 

      // set attribute to staff element 
      /* 
      * Attr attr = doc.createAttribute("id"); attr.setValue("1"); 
      * staff.setAttributeNode(attr); 
      */ 
      // shorten way 
      // staff.setAttribute("id", "1"); 

      // firstname elements 


      if (savename.equals("none")) { 

      } else { 
       Element nameelement = doc.createElement("name"); 
       nameelement.appendChild(doc.createTextNode(savename)); 
       outfit.appendChild(nameelement); 
      } 
      if (hatloc.equals("none")) { 

      } else { 
       Element hatelement = doc.createElement("hat"); 
       hatelement.appendChild(doc.createTextNode(hatloc)); 
       outfit.appendChild(hatelement); 
      } 

      if (shirtloc.equals("none")) { 

      } else { 
       Element shirtelement = doc.createElement("shirt"); 
       shirtelement.appendChild(doc.createTextNode(shirtloc)); 
       outfit.appendChild(shirtelement); 
      } 
      if (pantloc.equals("none")) { 

      } else { 
       Element pantselement = doc.createElement("pants"); 
       pantselement.appendChild(doc.createTextNode(pantloc)); 
       outfit.appendChild(pantselement); 
      } 

      if (shoeloc.equals("none")) { 

      } else { 
       Element shoeselement = doc.createElement("shoes"); 
       shoeselement.appendChild(doc.createTextNode(shoeloc)); 
       outfit.appendChild(shoeselement); 
      } 

      if (acc1loc.equals("none")) { 

      } else { 
       Element accelement = doc.createElement("accessories"); 
       accelement.appendChild(doc.createTextNode(acc1loc)); 
       outfit.appendChild(accelement); 
      } 

      if (acc2loc.equals("none")) { 

      } else { 
       Element acc2element = doc.createElement("accessories2"); 
       acc2element.appendChild(doc.createTextNode(acc2loc)); 
       outfit.appendChild(acc2element); 
      } 

      if (beltloc.equals("none")) { 

      } else { 
       Element beltelement = doc.createElement("belt"); 
       beltelement.appendChild(doc.createTextNode(beltloc)); 
       outfit.appendChild(beltelement); 
      } 

      if (dressloc.equals("none")) { 

      } else { 
       Element dresselement = doc.createElement("dress"); 
       dresselement.appendChild(doc.createTextNode(dressloc)); 
       outfit.appendChild(dresselement); 
      } 

      if (jacketloc.equals("none")) { 

      } else { 
       Element jacketelement = doc.createElement("jacket"); 
       jacketelement.appendChild(doc.createTextNode(jacketloc)); 
       outfit.appendChild(jacketelement); 
      } 

      // write the content into xml file 
      TransformerFactory transformerFactory = TransformerFactory 
        .newInstance(); 
      Transformer transformer = transformerFactory.newTransformer(); 
      DOMSource source = new DOMSource(doc); 
      StreamResult result = new StreamResult(
        new File(Environment.getExternalStorageDirectory() 
          + File.separator + "images" + File.separator 
          + "newxml.xml")); 
      transformer.transform(source, result); 

     } catch (ParserConfigurationException pce) { 
      pce.printStackTrace(); 
     } catch (TransformerException tfe) { 
      tfe.printStackTrace(); 
     } 

     hatloc = "none"; 
     shirtloc = "none"; 
     pantloc = "none"; 
     shoeloc = "none"; 
     acc1loc = "none"; 
     acc2loc = "none"; 
     beltloc = "none"; 
     dressloc = "none"; 
     jacketloc = "none"; 
     savename = "none"; 

    } 

一切工作正常,直到我到了下面的這一節。我認爲這是我創建的xml文件被寫入的部分。有誰知道一種方法來做到這一點,在Android中工作?

代碼中斷與TransformerFactoryTransformerDOMSourceStreamResultTransformerException

// write the content into xml file 
    TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
    Transformer transformer = transformerFactory.newTransformer(); 
    DOMSource source = new DOMSource(document); 
    StreamResult result = new StreamResult(
      new File(Environment.getExternalStorageDirectory() + File.separator 
        + "images" + File.separator + "newxml.xml")); 
    transformer.transform(source, result); 

回答

2

我認爲Transfomer類不包含在您正在使用的Android API中。 爲了避免使用Transformer,你應該手動迭代你的xml樹,否則你可以依賴一些外部庫。你應該看看here

+0

我可以做到這一點,通過使用我現有的代碼在頂部,然後替換底部部分? – Peter 2011-04-04 21:59:50

+0

@彼得我不是一個機器人專家,但我認爲是。 – javanna 2011-04-04 22:13:26

+0

您保存了我的生活,從該代碼行我的程序控制根本不會返回。 – 2013-08-31 11:36:41