在XML中写入数据

            XmlDocument xmldoc = new XmlDocument();
            //声明节
            XmlDeclaration dec = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
            xmldoc.AppendChild(dec);
            //加入一个根节点
            XmlElement oneNode = xmldoc.CreateElement("CATALOG");
            //创建节点
            XmlElement twoNode = xmldoc.CreateElement("CD");


            XmlElement twoNodeone = xmldoc.CreateElement("TITLE");
            twoNodeone.InnerText = "Empire Burlesque";
            twoNode.AppendChild(twoNodeone);//添加到CD节点下面


            XmlElement twoNodeone1 = xmldoc.CreateElement("ARTIST");
            twoNodeone1.InnerText = "Bob Dylan";
            twoNode.AppendChild(twoNodeone1);//添加到CD节点下面


            XmlElement twoNodeone2 = xmldoc.CreateElement("COUNTRY");
            twoNodeone2.InnerText = "USA";
            twoNode.AppendChild(twoNodeone2);//添加到CD节点下面


            XmlElement twoNodeone3 = xmldoc.CreateElement("COMPANY");
            twoNodeone3.InnerText = "Columbia";
            twoNode.AppendChild(twoNodeone3);//添加到CD节点下面


            XmlElement twoNodeone4 = xmldoc.CreateElement("YEAR");
            twoNodeone4.InnerText = "10.90";
            twoNode.AppendChild(twoNodeone4);//添加到CD节点下面


            XmlElement twoNodeone5 = xmldoc.CreateElement("PRICE");
            twoNodeone5.InnerText = "1985";
            twoNode.AppendChild(twoNodeone5);//添加到CD节点下面


            oneNode.AppendChild(twoNode);//添加到CATALOG节点下面
            //创建节点,第二个CD节点
            XmlElement twoNode1 = xmldoc.CreateElement("CD");


            XmlElement twoNodeone123 = xmldoc.CreateElement("TITLE");
            twoNodeone123.InnerText = "Empire Burlesque";
            twoNode1.AppendChild(twoNodeone123);//添加到CD节点下面


            XmlElement twoNodeone11 = xmldoc.CreateElement("ARTIST");
            twoNodeone11.InnerText = "Bob Dylan";
            twoNode1.AppendChild(twoNodeone11);//添加到CD节点下面


            XmlElement twoNodeone22 = xmldoc.CreateElement("COUNTRY");
            twoNodeone22.InnerText = "USA";
            twoNode1.AppendChild(twoNodeone22);//添加到CD节点下面


            XmlElement twoNodeone33 = xmldoc.CreateElement("COMPANY");
            twoNodeone33.InnerText = "Columbia";
            twoNode1.AppendChild(twoNodeone33);//添加到CD节点下面


            XmlElement twoNodeone44 = xmldoc.CreateElement("YEAR");
            twoNodeone44.InnerText = "10.90";
            twoNode1.AppendChild(twoNodeone44);//添加到CD节点下面


            XmlElement twoNodeone45 = xmldoc.CreateElement("PRICE");
            twoNodeone45.InnerText = "1985";
            twoNode1.AppendChild(twoNodeone45);//添加到CD节点下面


            oneNode.AppendChild(twoNode1);//添加到CATALOG节点下面
            xmldoc.AppendChild(oneNode);
            xmldoc.Save("1.xml");//保存xml 
XML 写入数据可以使用以下步骤: 1. 创建一个 DocumentBuilder 对象来创建 DOM 文档树。 2. 创建一个 Document 对象,用于表示整个 XML 文档。 3. 创建根元素节点,并添加到 Document 对象。 4. 创建子元素节点,并添加到根元素节点。 5. 创建元素属性,并将其添加到元素节点。 6. 创建文本节点,并将其添加到元素节点。 7. 将 Document 对象写入 XML 文件。 下面是一个 Java 示例代码: ```java import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Text; public class WriteXML { public static void main(String[] args) throws Exception { // 创建 DocumentBuilder 对象 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); // 创建 Document 对象 Document doc = builder.newDocument(); // 创建根元素节点 Element root = doc.createElement("root"); doc.appendChild(root); // 创建子元素节点 Element child = doc.createElement("child"); root.appendChild(child); // 创建元素属性 child.setAttribute("name", "value"); // 创建文本节点 Text text = doc.createTextNode("Hello, world!"); child.appendChild(text); // 将 Document 对象写入 XML 文件 File file = new File("output.xml"); javax.xml.transform.TransformerFactory.newInstance() .newTransformer().transform(new javax.xml.transform.dom.DOMSource(doc), new javax.xml.transform.stream.StreamResult(file)); } } ``` 运行该程序后,将在当前目录下生成一个名为 `output.xml` 的 XML 文件,其内容如下: ```xml <?xml version="1.0" encoding="UTF-8" standalone="no"?> <root> <child name="value">Hello, world!</child> </root> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值