如何在Java中使用DOM使用XHTML标签将自定义doctype标签添加到xml?

时间:2018-12-14 07:15:26

标签: java xml dom xhtml doctype

我已经使用Java中的DOM创建了一个XML文档,并将XHTML标记插入到xml文档中。现在,我想像这样添加文档类型:

<!DOCTYPE example [ <!ATTLIST xsl:stylesheet id ID #REQUIRED> ]>

我试图将其附加为字符串,但是没有用。

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

0 个答案:

没有答案