java 生成xml并转为字符串的方法

时间:2021-10-08 23:49:30

如下所示:

java" id="highlighter_799350">
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import javax.xml.parsers.documentbuilderfactory;
import javax.xml.parsers.documentbuilder;
import javax.xml.transform.transformerfactory;
import javax.xml.transform.outputkeys;
import javax.xml.transform.transformer;
import javax.xml.transform.dom.domsource;
import javax.xml.transform.stream.streamresult;
 
import org.apache.commons.io.output.bytearrayoutputstream;
 
import org.w3c.dom.document;
import org.w3c.dom.element;
 
public class test {
    
    private string createxml() {
        string xmlstring = "";
 
        documentbuilderfactory factory = documentbuilderfactory.newinstance();
        try {
            documentbuilder builder = factory.newdocumentbuilder();
            document document = builder.newdocument();
            document.setxmlstandalone(true);
 
            element iteminfo = document.createelement("iteminfo");
            document.appendchild(iteminfo);
 
            element itemstatistics = document.createelement("itemstatistics");
            itemstatistics.settextcontent("商品统计");
            iteminfo.appendchild(itemstatistics);
 
            element items = document.createelement("items");
            iteminfo.appendchild(items);
            // 此处可以循环添加
            element item = document.createelement("item");
            items.appendchild(item);
 
            element itemname = document.createelement("itemname");
            itemname.settextcontent("iphone");
            item.appendchild(itemname);
 
            element itemnum = document.createelement("itemnum");
            itemnum.settextcontent("3");
            item.appendchild(itemnum);
 
            element itemvalue = document.createelement("itemvalue");
            itemvalue.settextcontent("1000000");
            item.appendchild(itemvalue);
 
            element remark = document.createelement("remark");
            remark.settextcontent("配送");
            iteminfo.appendchild(remark);
 
            transformerfactory transfactory = transformerfactory.newinstance();
            transformer transformer = transfactory.newtransformer();
            transformer.setoutputproperty(outputkeys.indent, "yes");
            domsource domsource = new domsource(document);
 
            // xml transform string
            bytearrayoutputstream bos = new bytearrayoutputstream();
            transformer.transform(domsource, new streamresult(bos));
            xmlstring = bos.tostring();
            system.out.println(xmlstring);
        } catch (exception e) {
            e.printstacktrace();
        }
 
        return xmlstring;
    }
}

结果:

?
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<iteminfo>
<itemstatistics>商品统计</itemstatistics>
<items>
<item>
<itemname>iphone</itemname>
<itemnum>3</itemnum>
<itemvalue>1000000</itemvalue>
</item>
</items>
<remark>配送</remark>
</iteminfo>

去掉头文件

?
1
<?xml version="1.0" encoding="utf-8"?>

的办法暂时只能想到:

?
1
xmlstring.substring(38)

以上这篇java 生成xml并转为字符串的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/cat_book_milk/article/details/52954637