Java 如何在XML中创建新元素?
我尝试使用以下代码:Java 如何在XML中创建新元素?,java,xml,dom,Java,Xml,Dom,我尝试使用以下代码: public static void sendXml(String result, int operationN) throws ParserConfigurationException, SAXException, IOException{ String filepath = "journal.xml"; DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstan
public static void sendXml(String result, int operationN) throws ParserConfigurationException, SAXException, IOException{
String filepath = "journal.xml";
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filepath);
doc.getFirstChild();
doc.getElementsByTagName("command").item(0);
Element res = doc.createElement("result");
res.setTextContent(result);
doc.appendChild(res);
}
我肯定知道,journal.xml
和result
一切正常。我有异常HIERARCHY\u REQUEST\u ERR:试图在org.apache.xerces.dom.CoreDocumentImpl.insertBefore(未知源)插入不允许的节点
位于org.apache.xerces.dom.NodeImpl.appendChild(未知源)
。我做错了什么
我有这样的结构:
<?xml version="1.0"?>
<config>
<command> Check title (Total posts,Total topics,Total members,Our newest member)
// here i need result
</command>
<command> Check login
</command>
</config>
检查标题(帖子总数、主题总数、会员总数、我们的最新会员)
//这里我需要结果
检查登录
您最后应该将新创建的元素附加到文档中:
doc.appendChild(res);
你可以这样做
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
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;
import org.w3c.dom.Node;
public class WriteXMLFile {
public static void sendXml(String result, int operationN) throws Exception, IOException{
String filepath = "file.xml";
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filepath);
Node tonode=doc.getElementsByTagName("command").item(0);
Element res = doc.createElement("result");
res.setTextContent(result);
tonode.appendChild(res);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result1 = new StreamResult(new File("file2.xml"));
// Output to console for testing
StreamResult result3 = new StreamResult(System.out);
transformer.transform(source, result1);
System.out.println("File saved!");
}
public static void main(String argv[]) {
try {
WriteXMLFile writeXMLFile=new WriteXMLFile();
writeXMLFile.sendXml("hitest", 1);
} catch (Exception pce) {
pce.printStackTrace();
}
}
}
file2.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<config>
<command>
Check title (Total posts,Total topics,Total members,Our newest member)
<result>hitest</result>
</command>
<command> Check login
</command>
</config>
检查标题(帖子总数、主题总数、会员总数、我们的最新会员)
希特勒
检查登录
u需要将该元素添加到doc中,如doc.appendChild(res)。。如何打印…到concole或xml文件..post total Code我添加异常和文件结构您是否要为已经包含文本内容的标记添加子标记(即检查标题(总帖子、总主题、总成员、我们的最新成员))…不是吗?是的,正是这样,这将是命令的结果谢谢,我修改了一个小问题,这就是我从file.xml中读取并写入file2.xml的内容