我正在开发一个 android 应用程序,我想读取和编辑存储在设备内部存储器中的 xml 文件(该文件通过 usb 传输到设备的特定文件夹,而不是由应用程序创建)。为此,我使用了 Java 的 DOMxmlParser,但是当我想保存更新版本的 xml 文件时遇到了问题。我已经在我的 PC 上测试了我的编辑功能(作为一个简单的 Java 应用程序)并且它工作完美,但是当我在 android 应用程序中使用完全相同的代码但 xml 文件没有发生任何变化并且我没有错误时。我也想要明确该文件是可访问的,因为我可以从应用程序内部打印其数据。请帮忙
这是一些代码:
list
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tsiskos.training1" >
<uses-sdk android:minSdkVersion="11" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.DarkActionBar" >
<activity
android:name=".MyActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DisplayMessageActivity"
android:label="@string/app_name"
android:parentActivityName=".MyActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.tsiskos.training1.MyActivity" />
</activity>
<activity
android:name=".EditContact"
android:label="@string/title_activity_edit_contact" >
</activity>
</application>
XML编辑功能
public void editContact(String number,String oldName,String oldLimit,String newName,String newLimit) {
try {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(new File(xmlPath));
NodeList numbers = xmlDocument.getElementsByTagName("number");
for (int i = 0; i < numbers.getLength(); i++) {
if (((Element) numbers.item(i)).getAttribute("id").equals(number)) {
Element currNumber = (Element) numbers.item(i);
NodeList childs = currNumber.getChildNodes();
for (int j = 0; j < childs.getLength(); j++) {
Node currElement = childs.item(j);
if (currElement.getNodeName().equals("contact")) {
NodeList currContact = currElement.getChildNodes();
for (int k = 0; k < currContact.getLength(); k++) {
Node currContactChild = currContact.item(k);
String currNode = currContactChild.getNodeName();
String currValue = currContactChild.getTextContent();
if (currNode.equals("name") && currValue.equals(oldName)) {
currContactChild.setTextContent(newName);
}
if (currNode.equals("limit") && currValue.equals(oldLimit)) {
currContactChild.setTextContent(newLimit);
}
}
}
}
}
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(xmlDocument);
StreamResult result = new StreamResult(new File(xmlPath));
transformer.transform(source, result);
return xmlDocument;
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
我确定的是文件的读取和解析是成功的。 我是否错过了与 Android 文件系统相关的重要内容? 提前致谢。
最佳答案
显然问题出在 list 文件上。我尝试将结果写入字符串,然后写入 Sanj 评论的文件(调试提示:P),我得到了权限被拒绝的错误,这是我之前看不到的。 虽然我已经包含了写外部权限,但我找到了 this post 的答案。 建议使用权限标签的不同位置。
那么诀窍是移动
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
就在 list 文件结束之前。
关于java - 无法编辑/更新 xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29540800/