简体   繁体   中英

JAVA DOM API Processing inscrution and doctype before XML prolog

I am working on DOM API in JAVA, i have a problem, how may i add Precessing Instruction (XSLT Style Sheet) and DOCTYPE (Document type) after XML Prolog
each one in new line please?

eg :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE FICHES SYSTEM "docform.dtd">
<?xml-stylesheet type="text/xsl" href="stylesheet.xsl"?>

You need to create the DOCTYPE when creating the document, see http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-102161490 which defines a method createDocumentType on the implementation to create a DOCTYPE and a method createDocument to create a document taking the DOCTYPE as one parameter.

Thus you need

DocumentType docType = implementation.createDocumentType("FICHES", null, "docform.dtd");
Document doc = implementation.createDocument(null, "FICHES", docType);

That way you now have a DOM document doc with a DOCTYPE node and a root element named FICHES , then you can create and insert the processing instruction:

doc.insertBefore(doc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"stylesheet.xsl\""), doc.getDocumentElement());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM