Saving XML Documents

From: Stefan Schulz (terra_at_spacetime.de)
Date: 07/31/04


Date: Sat, 31 Jul 2004 12:16:05 +0200

Hi there

I have a Problem.

I read an XML file by the following sequence of method invocations
        DocumentBuilderFactory builderFactory =
                DocumentBuilderFactory.newInstance();

         builderFactory.setValidating(true);
         builderFactory.setExpandEntityReferences(true);
         builderFactory.setIgnoringComments(true);

         DocumentBuilder builder =
                builderFactory.newDocumentBuilder();
         builder.setErrorHandler(new ErrorHandler(){
                // ...
         });

         root = builder.parse(xmlFile);

which correctly includes entities and validates against the DTD given in
the <!DOCTYPE > declaration. However, once i am done with the document,
and possibly made modifications, i try to save it as follows:

             DOMSource src = new DOMSource(root);
             StreamResult res = new StreamResult(xmlFile);

             TransformerFactory transFactory =
                TransformerFactory.newInstance();
             Transformer trans = transFactory.newTransformer();
             trans.transform(src, res);

Which produces a file with all entities expanded, all default values for
attributes filled in, and with _no_doctype_declaration_. This will of
course not be cleanly parsable with my parser. Since this seems to be
the wrong way to go about things, can someone point me into the right
direction?

If possible, i would like to remain in the standard class library.

See you
Stefan



Relevant Pages

  • Re: How to read an XML file in Visual C++ 6
    ... the DTD could be part of the XML file itself ... I'd vote for putting it in the resources. ... It makes no sense to put a remote URL into a file which is going to be used locally. ...
    (microsoft.public.vc.mfc)
  • Serializing from XML with remote DTD
    ... I have an XML file which I am serializing into a class using the ... the XML file has a dtd in it that points to a remote ... mean, it's not trying to validate it, so why ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Convert flat file to XML document
    ... >I am a newbee and have a comma seperated flat-file and a DTD. ... >XML file as per the the definition in the DTD. ... As required add extra processing to your program so that you can ... The ultimate truth is that there is no ultimate truth ...
    (microsoft.public.dotnet.languages.csharp)
  • How to get Java to read in XML file and parse it against DTD?
    ... When using Java to read in an XML file, ... the XML file to be parsed against the specified DTD. ... I have an existing Java application that allows users to modify values ...
    (comp.lang.java.programmer)
  • Re: Confused About Validation
    ... DTDs have to be included or referred from an XML file. ... setting the validation type to DTD. ... > saying that Doctype is not defined. ... then use it to load / validate the xml files? ...
    (microsoft.public.dotnet.xml)