java - 如何从 java 或 Android 中的字符串制作 XML 文档?

标签 java android xml file

我正在尝试从 Java 创建一个动态生成的 XML 文件。这是我尝试使用的代码:

try{
            BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

            System.out.println("how many elements: ");
            String str = bf.readLine();
            int no = Integer.parseInt(str);

            System.out.println("enetr root: ");
            String root = bf.readLine();

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document d1 = db.newDocument();
            Element e1 = d1.createElement(root);
            d1.appendChild(e1);

            for (int i = 0; i < no; i++) {
                System.out.println("enter element: ");
                String element = bf.readLine();

                System.out.println("enter data: ");
                String data = bf.readLine();
                Element em = d1.createElement(element);
                em.appendChild(d1.createTextNode(data));

                e1.appendChild(em);
            }
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            DOMSource source = new DOMSource(d1);

            File file = new File("src\\xml\\copy.xml");
            System.out.println(file);

            if(!file.exists()){
                file.createNewFile();
            }
            OutputStream outputStream = new FileOutputStream(file);
            StreamResult result = new StreamResult(outputStream);
            transformer.transform(source, result);
            outputStream.close();

        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            System.out.println("file not created");
        }

此代码运行良好。但是现在我有一个这样的字符串:

String xmlRecords = "<data><terminal_id>1000099999</terminal_id><merchant_id>10004444</merchant_id><merchant_info>Mc Donald's - Abdoun</merchant_info></data>";

我想从这个 xmlRecords 变量创建一个 .xml 文件。我该怎么做?

最佳答案

您可以像这样将 XML 字符串解析为 Document:

String xmlRecords = "<data><terminal_id>1000099999</terminal_id><merchant_id>10004444</merchant_id><merchant_info>Mc Donald's - Abdoun</merchant_info></data>";

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document d1 = builder.parse(new InputSource(new StringReader(xmlRecords)));

您可以保留问题中提到的文件编写部分,只需将 Document 实例的创建替换为上述代码即可。

关于java - 如何从 java 或 Android 中的字符串制作 XML 文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6547263/

相关文章:

java - 比较来自 Firebase 的数据

Android Lombok 1.18.0 插件在发布构建期间导致 IllegalArgumentException 错误

android - 如果自定义 ListView 行内容是动态的,如何使用 View 持有者?

excel - XML解析VBA excel(函数行,&MSXML2.DOMDocument)

java - 在java中使用正则表达式查找和替换url

java - 用Java实现决策树数据库

java - Java 中具有继承性的 Varargs

android - 如何确定要放置在 TextView 中的文本是否适合单行

jquery 设置 xml cdata

java - 使用抛出 SAXException 的方法编译类时出现问题