0

So I'm trying to create an xml document in my android application. I'm using the code that I used when writing a java application. I tried as shown below:

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";

    }

Everything worked fine until I got to this section below. I think it's the section where the xml file i created gets written. Does anyone know of a way to do this that works in android?

The code breaks with the TransformerFactory, Transformer, DOMSource, StreamResult and TransformerException.

    // 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);
javanna
  • 59,145
  • 14
  • 144
  • 125
Peter
  • 5,071
  • 24
  • 79
  • 115

1 Answers1

2

I think that the Transfomer class is not included in the Android API you're using. To avoid using Transformer you should manually iterate over your xml tree, otherwise you can rely on some external libraries. You should take a look here.

Community
  • 1
  • 1
javanna
  • 59,145
  • 14
  • 144
  • 125