Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从字符串创建XML_Java_Xml_String_Dom_Domdocument - Fatal编程技术网

Java 从字符串创建XML

Java 从字符串创建XML,java,xml,string,dom,domdocument,Java,Xml,String,Dom,Domdocument,我有一个要转换为xml的字符串列表。我将如何使用DOM来实现这一点?我不知道如何使用字符串创建文档,以用作生成XML的源 有人能给我一个生成文档的示例代码吗? 例如,XML类似于: ArrayList<String> fruits <Fruits> <fruit>Apple<fruit> <fruit>Grape<fruit> <Fruits> 为此,我将使用JAXB: @XmlRootElement

我有一个要转换为xml的字符串列表。我将如何使用DOM来实现这一点?我不知道如何使用字符串创建文档,以用作生成XML的

有人能给我一个生成文档的示例代码吗? 例如,XML类似于:

ArrayList<String> fruits

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

为此,我将使用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);
}
@XmlRootElement(name=“Fruits”)
@XmlAccessorType(XmlAccessType.FIELD)
公共静态类水果
{
@xmlement(name=“fruit”)
public List fruit=new ArrayList();
}
公共静态void main(字符串[]args)引发异常
{
水果=新水果();
水果。水果。添加(“苹果”);
水果。水果。添加(“葡萄”);
TransformerFactory transFact=TransformerFactory.newInstance();
Transformer serializer=transFact.newTransformer();
Properties props=新属性();
put(“方法”、“xml”);
道具放置(“缩进”,“是”);
serializer.setOutputProperties(props);
Source Source=newjaxbsource(JAXBContext.newInstance(Fruits.class),Fruits);
结果结果=新的StreamResult(系统输出);
转换(源、结果);
}

我将使用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);
}
@XmlRootElement(name=“Fruits”)
@XmlAccessorType(XmlAccessType.FIELD)
公共静态类水果
{
@xmlement(name=“fruit”)
public List fruit=new ArrayList();
}
公共静态void main(字符串[]args)引发异常
{
水果=新水果();
水果。水果。添加(“苹果”);
水果。水果。添加(“葡萄”);
TransformerFactory transFact=TransformerFactory.newInstance();
Transformer serializer=transFact.newTransformer();
Properties props=新属性();
put(“方法”、“xml”);
道具放置(“缩进”,“是”);
serializer.setOutputProperties(props);
Source Source=newjaxbsource(JAXBContext.newInstance(Fruits.class),Fruits);
结果结果=新的StreamResult(系统输出);
转换(源、结果);
}
ArrayList a=新的ArrayList();
a、 添加(“苹果”);a、 添加(“芒果”);
DocumentBuilderFactory docFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder=docFactory.newDocumentBuilder();
Document doc=docBuilder.newDocument();
元素根=doc.createElement(“果实”);
doc.appendChild(根);
for(字符串名称:a){
元素水果=doc.createElement(“水果”);
export.appendChild(doc.createTextNode(name));
根附子(果);
}
TransformerFactory TransformerFactory=TransformerFactory.newInstance();
Transformer Transformer=transformerFactory.newTransformer();
DOMSource=新的DOMSource(doc);
StreamResult=newstreamresult(新文件(“C:\\File.xml”);
变换(源、结果);
ArrayList a=新的ArrayList();
a、 添加(“苹果”);a、 添加(“芒果”);
DocumentBuilderFactory docFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder=docFactory.newDocumentBuilder();
Document doc=docBuilder.newDocument();
元素根=doc.createElement(“果实”);
doc.appendChild(根);
for(字符串名称:a){
元素水果=doc.createElement(“水果”);
export.appendChild(doc.createTextNode(name));
根附子(果);
}
TransformerFactory TransformerFactory=TransformerFactory.newInstance();
Transformer Transformer=transformerFactory.newTransformer();
DOMSource=新的DOMSource(doc);
StreamResult=newstreamresult(新文件(“C:\\File.xml”);
变换(源、结果);

如果确实要使用“水果”列表中的字符串作为源创建文档:

    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())));

如果确实要使用“水果”列表中的字符串作为源创建文档:

    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())));

结果不是我所期望的。输出为
applegrapeant
。我希望每个水果都放在一个单独的
标签中,因为你可能做错了什么。看看我更新的答案,我已经删除了注释并添加了迭代器代码。结果不是我所期望的。输出为
applegrapeant
。我希望每个水果都放在一个单独的
标签中,因为你可能做错了什么。看看我的更新答案,我已经删除了注释并添加了迭代器代码。使用JAXB(JSR-222)实现,您只需使用
Marshaller
将对象写入所需的XML输出,而不是使用
转换器。在将样式表应用于JAXB源代码时,您给出的方法非常有用:@BlaiseDoughan实际上,我只是复制了OP的代码片段。否则,我会使用
JAXB.marshall(fruits,System.out)
。使用JAXB(JSR-222)实现,您可以使用
Marshaller
将对象写入所需的XML输出,而不是使用
转换器。在将样式表应用于JAXB源代码时,您给出的方法非常有用:@BlaiseDoughan实际上,我只是复制了OP的代码片段。否则,我只会使用
JAXB.marshal(fruits,System.out)