简体   繁体   English

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

[英]How to make XML document from string in java or Android?

I am trying to create a dynamically generated XML file from Java.我正在尝试从 Java 创建一个动态生成的 XML 文件。 This is the code that I am trying to use:这是我尝试使用的代码:

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");
        }

This code works very well.这段代码工作得很好。 But now I have a string as such:但现在我有一个这样的字符串:

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

I want to create a .xml file from this xmlRecords variable.我想从此xmlRecords变量创建一个.xml文件。 How do I do this?我该怎么做呢?

You can parse an XML string to a Document like this:您可以将 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)));

You can keep the file writing part mentioned in your question, just replace the creation of the Document instance with above code.您可以保留问题中提到的文件写入部分,只需将Document实例的创建替换为上述代码即可。

Here is another solution if you are using XmlPullParser :如果您使用的是XmlPullParser ,这是另一种解决方案:

XmlPullParser parser = Xml.newPullParser();
parser.setInput(new StringReader("<your><xml><string>Hello</string></xml></your>"));

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

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