简体   繁体   English

Java / Android-读取,处理XML文件并将其保存到内部存储中

[英]Java / Android - Read, Manipulate and save XML file to internal Storage

I'm developing an Android Application which will make use of an file System where Data will be stored in xml - files.. 我正在开发一个Android应用程序,它将使用文件系统,数据将存储在xml文件中。

I'm successfully reading the according file and altering the Nodes (the Values are changed - if viewing in debugger) but no changes are made to my locally saved file.. 我已经成功读取了相应的文件并更改了节点(值已更改-如果在调试器中查看),但是未对本地保存的文件进行任何更改。

Process: 1. Read/open local file //done 2. Change DOM NODE //done 3. Save changes in my local file <- fails 过程:1.读取/打开本地文件//完成2.更改DOM NODE //完成3.将更改保存在本地文件中<-失败

So far I know that I will have to use the TransformerFactory after following some Tutorials and other code snippets the changes are still not saved to my file.. 到目前为止,我知道在遵循一些教程和其他代码段之后,仍将不得不使用TransformerFactory,更改仍未保存到我的文件中。

Can someone tell me what I'm doing wrong? 有人可以告诉我我在做什么错吗?

My Code: 我的代码:

try {
        File path = a.getFilesDir();
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        Document doc = docBuilder.parse(path.getPath() + "/" + FILENAME_PERMISSIONS);

        NodeList myCourses = doc.getElementsByTagName(NODE_COURSE);

        for (int i = 0; i < myCourses.getLength(); i++) {
            Node course = myCourses.item(i);
            Element e = (Element)myCourses.item(i);

            Node myPermission = course.getFirstChild();
            String name = e.getAttribute(ATTR_NAME);

            switch (name){
                case FLAG_CrossCult:
                    myPermission.setTextContent(boolToString(crossCult));
                    break;

                case FLAG_KAIROS:
                    myPermission.setTextContent(boolToString(kairos));

                    break;

                case FLAG_UnfinStory:
                    myPermission.setTextContent(boolToString(unfinStory));

                    break;

                case FLAG_EmpToInf:
                    myPermission.setTextContent(boolToString(empToInf));

                    break;

                case FLAG_YouthKairos:
                    myPermission.setTextContent(boolToString(youthKairos));

                    break;
            }

            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            DOMSource source = new DOMSource(doc);

            StreamResult streamResult = new StreamResult(new File(path.getPath() + "/" + FILENAME_PERMISSIONS));
            transformer.transform(source,streamResult);





        }


    }
    catch (ParserConfigurationException e){

    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }catch(TransformerConfigurationException e){
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

I found the Problem ... 我发现了问题...

The Code was almost correct.. 2 changes needed to be done: 该守则几乎是正确的。需要做2处更改:

1: need to use "new File()" 1:需要使用“ new File()”

String myPath = path.getPath() + "/" + FILENAME_PERMISSIONS;
Document doc = docBuilder.parse(new File(myPath));

2: I was writing to the wrong Node 2:我写错了节点

//Node myPermission = course.getFirstChild();
//is now:

NodeList permissionList = e.getElementsByTagName(NODE_PERMISSION);
Node myPermission = permissionList.item(0);

I'll leave the question here incase someone needs to do the same.. 我将问题留在这里,以防有人需要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM