将 XML 格式保存为字符串而不是文件

Save XML format to a string instead of a file(将 XML 格式保存为字符串而不是文件)
本文介绍了将 XML 格式保存为字符串而不是文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我使用 DocumentBuilderFactory 创建 XML 文件,如下所示:如何在 Java 中创建具有特定结构的 XML 文件.

I used the DocumentBuilderFactory to create XML file as done here: How to create XML file with specific structure in Java.

我不想将其保存到文件中,而是将结果存储为 java 字符串.如何实现.

Instead of saving it to a file, I want to store the result as a java String. How to achieve that.

复制:如何转换组织.w3c.dom.Document 对象转换成字符串?

推荐答案

这个例子可以帮到你.

import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class DOMBasicDoc {
    public static void main(String args[]) {
        try {
            String[] input = {"John Doe,123-456-7890", "Bob Smith,123-555-1212"};
            String[] line = new String[2];
            DocumentBuilderFactory dFact = DocumentBuilderFactory.newInstance();
            DocumentBuilder build = dFact.newDocumentBuilder();
            Document doc = build.newDocument();
            Element root = doc.createElement("root");
            doc.appendChild(root);
            Element memberList = doc.createElement("members");
            root.appendChild(memberList);
            for (int i = 0; i < input.length; i++) {
                line = input[i].split(",");
                Element member = doc.createElement("member");
                memberList.appendChild(member);
                Element name = doc.createElement("name");
                name.appendChild(doc.createTextNode(line[0]));
                member.appendChild(name);
                Element phone = doc.createElement("phone");
                phone.appendChild(doc.createTextNode(line[1]));
                member.appendChild(phone);
            }
            TransformerFactory tFact = TransformerFactory.newInstance();
            Transformer trans = tFact.newTransformer();

            StringWriter writer = new StringWriter();
            StreamResult result = new StreamResult(writer);
            DOMSource source = new DOMSource(doc);
            trans.transform(source, result);
            System.out.println(writer.toString());

        } catch (TransformerException ex) {
            System.out.println("Error outputting document");
        } catch (ParserConfigurationException ex) {
            System.out.println("Error building document");
        }
    }
}

这篇关于将 XML 格式保存为字符串而不是文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Reliable implementation of PBKDF2-HMAC-SHA256 for JAVA(PBKDF2-HMAC-SHA256 for JAVA 的可靠实现)
Correct way to sign and verify signature using bouncycastle(使用 bouncycastle 签名和验证签名的正确方法)
Creating RSA Public Key From String(从字符串创建 RSA 公钥)
Why java.security.NoSuchProviderException No such provider: BC?(为什么 java.security.NoSuchProviderException 没有这样的提供者:BC?)
Generating X509 Certificate using Bouncy Castle Java(使用 Bouncy Castle Java 生成 X509 证书)
How can I get a PublicKey object from EC public key bytes?(如何从 EC 公钥字节中获取 PublicKey 对象?)