0
왜 항상이 오류가 발생합니다. 기존 XML 파일에 데이터를 추가하려고합니다. here의 대답을 읽고 제안 된 내용을 시도했습니다. 하지만 여전히 성공하지 못했습니다.이 오류는 최상위 루트 요소가 일 뿐이라는 것을 의미합니다. 그러나 왜 나는이 오류가 발생하는지 모르겠습니다.Android에만 하나의 루트 요소가 허용됩니다. Xml 오류
이것은 내 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());
}
}
는 오류 로그 및/또는 로그 캣을 게시 할 수 – BlackHatSamurai