所以我正在尝试在我的Android应用程序中创建一个xml文档。我正在使用编写java应用程序时使用的代码。我尝试如下所示:
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?
代码符合TransformerFactory
,Transformer
,DOMSource
,StreamResult
和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);
答案 0 :(得分:2)
我认为Transfomer
类不包含在您正在使用的Android API中。
为避免使用Transformer
,您应手动迭代xml树,否则可以依赖某些外部库。你应该看看here。