写入xml

时间:2018-06-15 11:23:47

标签: android xml byte-order-mark

我有一个方法,其中.txt文件使用Scanner进行解析,使用DocumentBuilder重新组合,并转换为带有TransformerFactory的.xml文件。

一切正常,除了一点不便之外:以这种方式创建的文件包含我在其名称的开头就是BOM的内容。我在UTF-8编码。

它保存在%EF%BB%BFexample.xml下,而不是example.xml

我该如何避免?

编辑:正如您在评论中看到的那样,我被指出fileTitleScanner读取的第一行userText的可能性}可能包含UTF-8的BOM,结果是真的(再次,请参阅注释)。

private void writeXML() {
    try {
        File userText = new File(passedPath);

        Scanner scn = new Scanner(new FileInputStream(userText), "UTF-8");

        String separate = ";";
        String fileTitle = scn.nextLine();
        int indSepTitle = fileTitle.indexOf(separate);
        fileTitle = fileTitle.substring(0,indSepTitle);

        String fileOutputName = fileTitle+".xml";
        File mOutFile = new File(getFilesDir(), fileOutputName);

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        //root element
        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("Collection");
        doc.appendChild(rootElement);

        //List element
        Element listElement = doc.createElement("List");
        rootElement.appendChild(listElement);

        //set Attributes to listElement
        Attr attr = doc.createAttribute("name");
        attr.setValue(fileTitle);
        listElement.setAttributeNode(attr);

        while(scn.hasNext()) {
            String line = scn.nextLine();
            String[] parts = line.split(separate);

            //vocabulary element
            Element ringElement = doc.createElement("element_ring");
            listElement.appendChild(n1Element);

            //add 1st Element
            Element n1Element = doc.createElement("element1");
            natWord.appendChild(doc.createTextNode(parts[0]));
            ringElement.appendChild(n1Element);

            //add 2ndElement
            Element n2Element = doc.createElement("element2");
            forWord.appendChild(doc.createTextNode(parts[1]));
            ringElement.appendChild(n2Element);

            ...
            //add other Elements accordingly
            ...
            }

        //write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(mOutFile);

        transformer.transform(source, result);


    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

}

1 个答案:

答案 0 :(得分:1)

为了完成:

我包含以下简短代码,用于从字符串中删除BOM,该字符串被解压缩以用作正在创建的.xml文件的标题名称。

responseCode