Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这是我的密码;阅读是关于如何在android源代码中编写本地xml文件的编写和更新? 公共类LocalXMLActivity扩展活动{ /**在首次创建活动时调用*/ @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main);//设置布局..它引用main.xml //从布局文件夹。 输入流为; 文件建设者工厂; 文档生成器; 文件文件; NodeList allTitles=null; 字符串dataAll; 试一试{ //输入流用于读取字节 //打开android资源(res)文件夹并打开RawResource到raw //res(res/raw)下的文件夹 is=this.getResources().openRawResource(R.raw.data); //是读取从XMLDOM生成提示的解析器。 factory=DocumentBuilderFactory.newInstance(); //一旦获得此类的实例,就可以解析XML //来自各种输入源。这些输入源是 //输入流、文件、URL和SAX输入源 //从互联网获取数据的来源。 builder=factory.newDocumentBuilder(); //该文档表示整个HTML或XML文档。 //从概念上讲,它是文档树的根,并提供 //对文档数据的主要访问权限。 doc=builder.parse(is); //从xml检索“公司”节点 allTitles=doc.getElementsByTagName(“公司”); }捕获(ParserConfiguration异常e){ e、 printStackTrace(); }捕获(SAXE异常){ e、 printStackTrace(); }捕获(IOE异常){ e、 printStackTrace(); } dataAll=“”; 对于(int i=0;i_Android_Stream - Fatal编程技术网

这是我的密码;阅读是关于如何在android源代码中编写本地xml文件的编写和更新? 公共类LocalXMLActivity扩展活动{ /**在首次创建活动时调用*/ @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main);//设置布局..它引用main.xml //从布局文件夹。 输入流为; 文件建设者工厂; 文档生成器; 文件文件; NodeList allTitles=null; 字符串dataAll; 试一试{ //输入流用于读取字节 //打开android资源(res)文件夹并打开RawResource到raw //res(res/raw)下的文件夹 is=this.getResources().openRawResource(R.raw.data); //是读取从XMLDOM生成提示的解析器。 factory=DocumentBuilderFactory.newInstance(); //一旦获得此类的实例,就可以解析XML //来自各种输入源。这些输入源是 //输入流、文件、URL和SAX输入源 //从互联网获取数据的来源。 builder=factory.newDocumentBuilder(); //该文档表示整个HTML或XML文档。 //从概念上讲,它是文档树的根,并提供 //对文档数据的主要访问权限。 doc=builder.parse(is); //从xml检索“公司”节点 allTitles=doc.getElementsByTagName(“公司”); }捕获(ParserConfiguration异常e){ e、 printStackTrace(); }捕获(SAXE异常){ e、 printStackTrace(); }捕获(IOE异常){ e、 printStackTrace(); } dataAll=“”; 对于(int i=0;i

这是我的密码;阅读是关于如何在android源代码中编写本地xml文件的编写和更新? 公共类LocalXMLActivity扩展活动{ /**在首次创建活动时调用*/ @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main);//设置布局..它引用main.xml //从布局文件夹。 输入流为; 文件建设者工厂; 文档生成器; 文件文件; NodeList allTitles=null; 字符串dataAll; 试一试{ //输入流用于读取字节 //打开android资源(res)文件夹并打开RawResource到raw //res(res/raw)下的文件夹 is=this.getResources().openRawResource(R.raw.data); //是读取从XMLDOM生成提示的解析器。 factory=DocumentBuilderFactory.newInstance(); //一旦获得此类的实例,就可以解析XML //来自各种输入源。这些输入源是 //输入流、文件、URL和SAX输入源 //从互联网获取数据的来源。 builder=factory.newDocumentBuilder(); //该文档表示整个HTML或XML文档。 //从概念上讲,它是文档树的根,并提供 //对文档数据的主要访问权限。 doc=builder.parse(is); //从xml检索“公司”节点 allTitles=doc.getElementsByTagName(“公司”); }捕获(ParserConfiguration异常e){ e、 printStackTrace(); }捕获(SAXE异常){ e、 printStackTrace(); }捕获(IOE异常){ e、 printStackTrace(); } dataAll=“”; 对于(int i=0;i,android,stream,Android,Stream,xml文件:res/raw/data.xml public class LocalXMLActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setC

xml文件:res/raw/data.xml

public class LocalXMLActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); // set the layout.. it refers main.xml
                                       // from the layout folder.
        InputStream is;
        DocumentBuilderFactory factory;
        DocumentBuilder builder;
        Document doc;
        NodeList allTitles = null;
        String dataAll;
        try {

            // Input Stream is used to read the bytes. getResources refers to
            // the android resources(res)folder and open RawResource to the raw
            // folder under res. (res/raw)
            is = this.getResources().openRawResource(R.raw.data);
            // Is to read the parser that prodcues from XML DOM.
            factory = DocumentBuilderFactory.newInstance();
            // Once an instance of this class is obtained, XML can be parsed
            // from a variety of input sources. These input sources are
            // InputStreams, Files, URLs, and SAX InputSources. There are many
            // sources to take data from internet.
            builder = factory.newDocumentBuilder();
            // the Document represents the entire HTML or XML document.
            // Conceptually, it is the root of the document tree, and provides
            // the primary access to the document's data.
            doc = builder.parse(is);
            // Retrieving the "company" node from xml
            allTitles = doc.getElementsByTagName("Company");

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        dataAll = "";
        for (int i = 0; i < allTitles.getLength(); i++) {
            dataAll += allTitles.item(i).getChildNodes().item(0).getNodeValue() + "\n";
        }

        TextView tv = (TextView)findViewById(R.id.myTextView); // finds the
                                                               // widget"myTextView"
                                                               // from main.xml
        tv.setText(dataAll); // setting the dataAll value to myTextView.
    }
}

谷歌
搜索引擎/邮件/社交媒体
www.google.com
雅虎
搜索引擎/邮件
www.yahoo.com

上面的代码只是在读代码,我刚刚读的res/raw/data.XML中存储了一个XML文件,但是如何在本地XML文件中写入和更新,以及如何在内部读取源代码写入到本地XML文件中写入和更新呢?

嗯,老实说,这写得很混乱,但我的想法如下:

    <?xml version="1.0" ?>
<content>
    <item>
        <Company>Google</Company>
        <Feature>Search Engine / Mail / Social Media</Feature>
        <url>www.google.com</url>
    </item>
    <item>
        <Company>Yahoo</Company>
        <Feature>Search Engine / Mail</Feature>
        <url>www.yahoo.com</url>
    </item>
</content>

这会将SD卡中的文件写入/path/file.xml。您可以放入一个字节数组,对于小文件,您可以一次完成,如果您有一组大数据,您可以将其放入一个循环。

您可以在SD卡上创建一个新文件,如下所示:

FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + "/path/file.xml");
fos.write(byte_array_from_input);
fos.close();
File Directory=新文件(Environment.getExternalStorageDirectory())
+File.separator+“working”+File.separator+“accounts”);
File masterXMLfile=新文件(目录“MasterCategories.xml”);
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
//获取DocumentBuilder
DocumentBuilder docBuilder=null;
试一试{
docBuilder=factory.newDocumentBuilder();
}捕获(ParserConfiguration异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//创建空白DOM文档
Document doc=docBuilder.newDocument();
//创建根元素
元素根=doc.createElement(“类别”);
//所有这些都将被保存到xml树中
doc.appendChild(根);
for(int i=0;iFile Directory=  new File(Environment.getExternalStorageDirectory()
                        + File.separator + "working" +File.separator+"accounts");


File masterXMLfile = new File(Directory,"MasterCategories.xml");
                 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                  //Get the DocumentBuilder
                  DocumentBuilder docBuilder = null;
                try {
                    docBuilder = factory.newDocumentBuilder();
                } catch (ParserConfigurationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                  //Create blank DOM Document
                  Document doc = docBuilder.newDocument();


                  //create the root element
                  Element root = doc.createElement("Categories");
                  //all it to the xml tree
                  doc.appendChild(root);
                  for(int i =0; i < nodeArray.length;i++)
                  {
                      System.out.println("entered into if loop");
                      Element subroot = doc.createElement("Category");
                      root.appendChild(subroot);
                      Element nameNode = doc.createElement("Name");
                      nameNode.appendChild(doc.createTextNode(nodeArray[i]));
                      subroot.appendChild(nameNode);

                      Element thumbNailNode = doc.createElement("thumbnail");
                      thumbNailNode.appendChild(doc.createTextNode(nodeArray[i]+".jpg"));
                      subroot.appendChild(thumbNailNode);
                      Element descriptionNode = doc.createElement("description");
                      descriptionNode.appendChild(doc.createTextNode(nodeArray[i]+" data"));
                      subroot.appendChild(descriptionNode);
                  }
                  TransformerFactory transfactory = TransformerFactory.newInstance();
                    Transformer transformer= null;
                    try {
                        transformer = transfactory.newTransformer();
                    } catch (TransformerConfigurationException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

                    // create string from xml tree
                    // StringWriter sw = new StringWriter();
                    // StreamResult result = new StreamResult(sw);
                    FileWriter categoriesFW = null;
                    try {
                        categoriesFW = new FileWriter(masterXMLfile);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    StreamResult result = new StreamResult(categoriesFW);
                    DOMSource source = new DOMSource(doc);
                    try {
                        transformer.transform(source, result);
                    } catch (TransformerException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                if(!masterXMLfile.exists())
                {
                    try {
                        masterXMLfile.createNewFile();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }