为什么我总是收到此错误。我正在尝试将数据附加到现有的xml文件。我读了here答案并尝试了建议的内容。 但仍然没有成功。我知道这个错误意味着顶级根元素可以 只来一次。但为什么我得到这个错误我不知道。
这将是我的xml文件的结构。
<root>
<ip>ip1</ip>
<ip>ip2</ip>
</root>
ip标签将继续增加。这就是我试图读取数据并将其附加到现有文件的方式。
private void UpdateExistingXML(String ip,File file)
{
try
{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(file.toURI().toString()); // <--error here
// Get the root element
Node root= doc.getFirstChild();
org.w3c.dom.Element newip=doc.createElement("ip");
newip.appendChild(doc.createTextNode(ip));
root.appendChild(newip);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(file);
transformer.transform(source, result);
}
catch (ParserConfigurationException pce)
{
pce.printStackTrace();
}
catch (TransformerException tfe)
{
tfe.printStackTrace();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
catch (SAXException sae)
{
sae.printStackTrace();
}
catch (Exception e)
{
Log.e("eeee",e.getMessage());
}
}
这是我第一次创建xml文件的方式,它显示了根元素只插入一次。
private void CreateNewXML(String ip) throws FileNotFoundException
{
FileOutputStream fos=null ;
Log.i("Fileeee","new");
try
{
fos = openFileOutput("clients.xml", Context.MODE_PRIVATE);
}
catch(FileNotFoundException e)
{
Log.e("FileNotFoundException", "can't create FileOutputStream");
}
XmlSerializer serializer = Xml.newSerializer();
try {
serializer.setOutput(fos, "UTF-8");
serializer.startDocument(null, Boolean.valueOf(true));
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
serializer.startTag(null, "root");
serializer.startTag(null, "ip");
serializer.text(ip);
serializer.endTag(null, "ip");
serializer.endTag(null, "root");
serializer.endDocument();
serializer.flush();
fos.close();
}
catch (Exception e)
{
Log.e("Exceptionhaiiiiiiiiiii",e.getMessage());
}
}
答案 0 :(得分:0)
这看起来像你的问题:
Document doc = docBuilder.parse(file.toURI().toString()); // <--error here
您正在尝试解析文件路径,而不是文件。您需要创建一个inputStream
并解析它。
如果您从Web服务器获取文件,它可能如下所示:
URL url = new URL(DATAURL);
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(TIMEOUT);
conn.setConnectTimeout(CONNECT_TIMEOUT);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
DocumentBuilderFactory builderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try
{
builder = builderFactory.newDocumentBuilder();
}
catch (ParserConfigurationException e)
{
Log.e(TAG, "Parse Configuration issue", e);
throw new ServiceException("Service Exception Error");
}
catch (IllegalAccessError e)
{
Log.e(TAG, "Illegal Accessor Error", e);
throw new ServiceException("Service Exception Error");
}
try
{
// parse input from server
Document document = builder.parse(conn.getInputStream());
Element xmlElement = document.getDocumentElement();
NodeList recordNodes = xmlElement.getChildNodes();
// assign parsed data to listItem object
for (int i = 0; i < recordNodes.getLength(); i++)
{
Node record = recordNodes.item(i);
NodeList recordDetails = record.getChildNodes();
ListData listItem = new ListData();
for (int ii = 0; ii < recordDetails.getLength(); ii++)
{
Node detailItem = recordDetails.item(ii);
String detailType = detailItem.getNodeName();
String detailValue = detailItem.getTextContent();
.....