简体   繁体   English

从字符串创建XML

[英]Creating XML from Strings

I have a list of strings that i want to convert to xml. 我有一个要转换为xml的字符串列表。 How would I do so using DOM? 我将如何使用DOM? I'm not sure how to create a Document using the strings to use as a Source to generate the XML. 我不确定如何使用字符串作为Source来生成XML来创建文档。

Can anyone show me a sample code to generate a Document? 谁能给我看示例代码来生成文档? For example the XML would be like: 例如,XML如下所示:

ArrayList<String> fruits

<Fruits>
  <fruit>Apple<fruit>
  <fruit>Grape<fruit>
<Fruits>

I think the code would be: 我认为代码将是:

TransformerFactory transFact = TransformerFactory.newInstance();
Transformer serializer = transFact.newTransformer();
Properties props = new Properties();
props.put("method", "xml");
props.put("indent", "yes");
serializer.setOutputProperties(props);
Source source = new DOMSource(document); //need to create a document
Result result = new StreamResult(PrintWriter);  
    ArrayList<String> a = new ArrayList<String>();
    a.add("apple"); a.add("mango");

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

    Document doc = docBuilder.newDocument();
    Element root = doc.createElement("Fruits");
    doc.appendChild(root);

    for(String name: a){
        Element fruit = doc.createElement("fruit");
        fruit.appendChild(doc.createTextNode(name)); 
        root.appendChild(fruit);
    }
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File("C:\\file.xml"));

    transformer.transform(source, result);

I would use JAXB for this: 我将为此使用JAXB:

@XmlRootElement(name = "Fruits")
@XmlAccessorType(XmlAccessType.FIELD)
public static class Fruits
{
    @XmlElement(name = "fruit")
    public List<String> fruit = new ArrayList<String>();
}

public static void main(String[] args) throws Exception
{
    Fruits fruits = new Fruits();
    fruits.fruit.add("Apple");
    fruits.fruit.add("Grape");

    TransformerFactory transFact = TransformerFactory.newInstance();
    Transformer serializer = transFact.newTransformer();
    Properties props = new Properties();
    props.put("method", "xml");
    props.put("indent", "yes");
    serializer.setOutputProperties(props);
    Source source = new JAXBSource(JAXBContext.newInstance(Fruits.class), fruits);
    Result result = new StreamResult(System.out);
    serializer.transform(source, result);
}

if you really want to create a Document using strings in "fruits" list as a Source: 如果您真的想使用“水果”列表中的字符串作为源来创建文档:

    StringBuilder sb  = new StringBuilder();
    for(String s : fruits) {
        sb.append(s).append('\n');
    }
    DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
    Document doc = f.newDocumentBuilder().parse(new InputSource(new StringReader(sb.toString())));

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

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