Adding unparsed entity declaration to XML using Java

I am trying to generate an xml file similar to the following:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dmodule [
<!ENTITY ICN-XYZ897-YYY-12345678-P-AB123-00034-A-001-01 SYSTEM 'ICN-XYZ897-YYY-12345678-P-AB123-00034-A-001-01.cgm' NDATA cgm>
<!NOTATION cgm SYSTEM 'cgm'>
<!ENTITY % ISOEntities PUBLIC 'ISO 8879-1986//ENTITIES ISO Character Entities 20030531//EN//XML' 'http://www.s1000d.org/S1000D_4-0-1/ent/xml/ISOEntities'>
%ISOEntities;]>
<dmodule xmlns:dc="http://www.purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.s1000d.org/S1000D_4-0-1/xml_schema_flat/ipd.xsd">
....
<graphic boardno="ICN-XYZ897-YYY-12345678-P-AB123-00034-A-001-01"/>
....
</dmodule>

I have done a fair amount of searching and have not found anything that helps me understand how to generate the <!ENTITY ...> declarations or the <!NOTATION..> declarations. I have seen mention of using the DOM Level 3 Load/Save mechanisms, but all the examples I have gone over discussed how to incorporate a full internal DTD, and nothing of the entities. I am building an application that builds these XML files based on values pulled from a DB, and this is pretty much the last hurdle I have for creating the XML. Thanks in advance for any help that anyone can provide. I am sure someone will probably explain it and it will be something I have seen but failed to properly understand.

Answers


OK, I have come up with a solution. It's not exactly the prettiest, I'd even bet some would say it is a 'hack'. All that aside, it works and the XML is valid when I am done. Here is a simple little test program I wrote this afternoon:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;

public class fTesting {

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        try
        {
            int c = 0;
            File in = new File("c:/users/tom/desktop/filechangetest/test.xml");
            File out = new File("c:/users/tom/desktop/filechangetest/test2.xml");
            //array used to test when multiple ICN in XML, only one shown here
            String[] icns = new String[] {"ICN-XYZ897-YYY-12345678-P-AB123-00034-A-001-01"};

            Scanner read = new Scanner(in);

            BufferedWriter writer = new BufferedWriter(new FileWriter(out));

            while(read.hasNextLine())
            {
                c++;
                if(c == 1)
                {
                    writer.append(read.nextLine());
                }
                else if(c == 2)
                {
                    writer.append("<!DOCTYPE dmodule[\n");

                    for(int i = 0; i < icns.length; i++)
                    {
                        writer.append("<!ENTITY "+icns[i]+" " +
                                "SYSTEM '"+ icns[i] +".cgm' NDATA cgm>");
                    }

                    writer.append("<!NOTATION cgm SYSTEM 'cgm'>"+
                            "<!ENTITY % ISOEntities PUBLIC 'ISO 8879-1986//ENTITIES ISO Character Entities 20030531//EN//XML' " +
                            "'http://www.s1000d.org/S1000D_4-0-1/ent/xml/ISOEntities'>\n%ISOEntities;]>\n");

                }
                else
                    writer.append(read.nextLine());
            }
            read.close();
            writer.close();
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }

}

When run against the following XML (test.xml)

<?xml version="1.0" encoding="utf-8"?>
<dmodule xmlns:dc="http://www.purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.s1000d.org/S1000D_4-0-1/xml_schema_flat/ipd.xsd">
<child>
    <test>Some content</test>
</child>
</dmodule>

It creates this XML:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE dmodule[
<!ENTITY ICN-XYZ897-YYY-12345678-P-AB123-00034-A-001-01 SYSTEM 'ICN-XYZ897-YYY-12345678-P-AB123-00034-A-001-01.cgm' NDATA cgm>
<!NOTATION cgm SYSTEM 'cgm'>
<!ENTITY % ISOEntities PUBLIC 'ISO 8879-1986//ENTITIES ISO Character Entities 20030531//EN//XML' 'http://www.s1000d.org/S1000D_4-0-1/ent/xml/ISOEntities'>
%ISOEntities;]>
<dmodule xmlns:dc="http://www.purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.s1000d.org/S1000D_4-0-1/xml_schema_flat/ipd.xsd">
    <child>
        <test>Some content</test>
    </child>
</dmodule>

Now just to incorporate it into my larger application. Even though I ended up coming up with a solution, I'd still like to thank everyone that took the time to look at my question.


Need Your Help

check network connection while downloading file (c#)

c# networking download webclient

I would like to check my internet connection while using WebClient in order to download a file. (WebClient.DownloadFile()).