How to add a DOCTYPE in an XML filek with DOMDocument

AitorEGAitorEG Member Posts: 342
iHi everyone?
How can i add this line to an XML file, using DOMDocument methods?
<!DOCTYPE ESHIPPER SYSTEM 'https://XXXXXXXXXXXXXXXXX.dtd'>

For adding this
<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>

I use:


XMLDomInst:= XMLDocOut.createProcessingInstruction('xml','version="1.0" encoding="iso-8859-1" standalone="no"');

Which method could be used for adding the DOCTYPE?

Thank you very much!!

Answers

  • AitorEGAitorEG Member Posts: 342
    NavNab wrote: »

    Thanks for your answer. i was using that, in this way:
    docTypeXMlNode := XMLDocOut.CreateDocumentType(ESHIPPER SYSTEM 'https://iconnection.tnt.com/ShipperDTD2.0/EshipperIN2.dtd');
    
    where XMLDocOut is a DOMDocument. May be that is why it shows me he error that CreateDocumentType method doesn't exist. It is a "'Microsoft XML, v6.0'.DOMDocument" instead of a properly said XMLDocument. Any clues for this type of object?


  • AitorEGAitorEG Member Posts: 342
    These are the method that can be used on this objet:

    https://msdn.microsoft.com/en-us/library/ms757828(v=vs.85).aspx

    No hints about adding the DOCTYPE???
  • NavNabNavNab Member Posts: 181
    Hello @AitorEG

    Check here. It says
    The DocumentType node must also be inserted before the root element of the XmlDocument (if the document already has a root element, you cannot add a DocumentType node).
    And that's what was suggested in the first link:
    var xDocument = new XDocument(
      new XDocumentType(
        "smil",
        "-//W3C//DTD SMIL 2.0//EN",
        "http://www.w3.org/2001/SMIL20/SMIL20.dtd",
        null
      ),
      new XElement("Root") // --->Root is created after DocType
    );
    

    Your code should do something like this:
    doctype = XMLdoc.CreateDocumentType("book", "http://mydtd.com/book.dtd", "book.dtd", null);
    XMLdoc.AppendChild(doctype);
    

    Maybe someone can better help you if you share your code ?
  • AitorEGAitorEG Member Posts: 342
    Hi @NavNab

    This is my code:
    companyInfo.GET;
    
    CREATE(XMLDocOut,FALSE,TRUE);
    XMLDocOut.async(FALSE); 
    
    XMLDomInst:= XMLDocOut.createProcessingInstruction('xml','version="1.0" encoding="iso-8859-1" standalone="no"');
    XMLDocOut.appendChild(XMLDomInst);
    docTypeXMlNode := XMLDocOut.createDocType('ESHIPPER' ,null, 'https://XXXXXXXXXX/EYYYYY.dtd');
    EshiperXMlNode := XMLDocOut.createNode(1,'ESHIPPER','');
    XMLDocOut.appendChild(EshiperXMlNode);
    

    It looks like finally don't need to insert the doctype declaration, but I have to confirm that with the system owner later this morning. But anyway, it seems that the differences betwen XMLDocument and DOMDocument will create me a few more problem with the comunication, and when using http requests...

    Thank you again, really appreciate!!!
Sign In or Register to comment.