我使用java中的DOM创建了一个XML文档,并将XHTML标记插入到XML文档中。现在我要添加如下doctype:
]>
我试着把它作为一个字符串追加,但没有成功。
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
docFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
DOMImplementation domImpl = docBuilder.getDOMImplementation();
Document xmlDoc = domImpl.createDocument(null, "example", null);
xmlDoc.setXmlVersion("1.0");
xmlDoc.setXmlStandalone(true);
String xhtmlTag = "xhtml tags";
ByteArrayInputStream bis = new ByteArrayInputStream(xml.getBytes());
Document doc2 = docBuilder.parse(bis);
Node xhtml = xmlDoc.importNode(doc2.getDocumentElement(), true);
ProcessingInstruction pi = xmlDoc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"#stylesheet\"");
Element example = xmlDoc.getDocumentElement();
beyanname.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
example.setAttribute("xsi:noNamespaceSchemaLocation", "TEST.xsd");
example.setAttribute("lang", "az");
example.setAttribute("error", "xeta_yoxdur");
xmlDoc.insertBefore(pi, example);
example.appendChild(xhtml);