J
Jack Andersson
I'm using the code below to parse a DTD (which partly present below) and
then generating corresponding classes.
The problem I'm having now is that the generated classes seems to have the
CompanyQuery as a subclass to CGDocument and all the others as subclasses to
CGNode. However, my plan was to also have the CompanyResult as a subclass to
CGDocument, but I can't understand what I'm doing wrong. Please note that
the DTD is not present completely, because it is huge. Thus it may not
possible to parse it directly.
import java.io.File;
import java.net.URL;
import oracle.xml.parser.v2.DOMParser;
import oracle.xml.parser.v2.DTD;
import oracle.xml.parser.v2.XMLDocument;
import oracle.xml.parser.XMLParser;
import oracle.xml.classgen.*;
public class SampleMain2
{
public SampleMain2()
{
}
public static void main (String args[])
{
// PostCompanyQueryToTheca();
CreateReturnMetaData();
}
public static void parseDTD ()
{
dtdFile = "MyDtd.dtd";
String rootName = "CompanyQuery";
try
{
// Instantiate the parser
DOMParser parser = new DOMParser();
//XMLParser parser = new oracle.xml.parser.v2.XMLParser();
XMLDocument doc = null;
oracle.xml.parser.v2.DTD dtd_v2 = null;
oracle.xml.parser.DTD dtd_v1 = null;
oracle.xml.parser.XMLParser xmlParser = new XMLParser();
xmlParser.setErrorStream(System.out);
xmlParser.setValidationMode(false);
//xmlParser.parseDTD(fileToURL(dtdFile), rootName);
dtd_v2 = new oracle.xml.parser.v2.DTD();
dtd_v1 = (oracle.xml.parser.DTD) xmlParser.getDoctype();
parser.setDebugMode(true);
parser.setErrorStream(System.out);
//parser.setSchemaValidationMode(false);
//parser.setPreserveWhitespace(false);
//parser.setValidationMode(false);
parser.parseDTD(fileToURL(dtdFile), rootName);
dtd_v2 = (DTD)parser.getDoctype();
String doctype_name = null;
doctype_name = rootName;
// generate the Java files...
ClassGenerator generator = new ClassGenerator();
// set generate comments to true
generator.setGenerateComments(true);
// set output directory
generator.setOutputDirectory(".\\src\\DTDClasses\\");
//generator.setOutputDirectory(".\\src\\");
// set validating mode to true
generator.setValidationMode(true);
// generate java src
generator.generate(dtd_v2, doctype_name);
}
catch (Exception e)
{
System.out.println ("XML Class Generator: Error " + e.toString());
e.printStackTrace();
}
}
static public URL fileToURL(String sfile)
{
File file = new File(sfile);
String path = file.getAbsolutePath();
String fSep = System.getProperty("file.separator");
if (fSep != null && fSep.length() == 1)
path = path.replace(fSep.charAt(0), '/');
if (path.length() > 0 && path.charAt(0) != '/')
path = '/' + path;
try
{
return new URL("file", null, path);
}
catch (java.net.MalformedURLException e)
{
// According to the spec this could only happen if the file
// protocol were not recognized.
throw new Error("unexpected MalformedURLException");
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- Fråga mot företag -->
<!ELEMENT CompanyQuery EMPTY>
<!ATTLIST CompanyQuery CompEmployee CDATA #IMPLIED>
<!ATTLIST CompanyQuery Employee CDATA #IMPLIED>
<!-- Anrop till sökmotor, slår samman frågor -->
<!ELEMENT CompanyCompose (CompanyQuery+)>
<!ATTLIST CompanyCompose CustomerId CDATA #REQUIRED>
<!ATTLIST CompanyCompose Duplicates (YES |
VATNO |
CFARNR |
NAME |
NAMELOCALITY |
NAMEADDRESS |
TELEPHONE)
"YES">
<!ATTLIST CompanyCompose SendRecords (NO|DIRECT|FILE)
"NO">
<!ATTLIST CompanyCompose FromRecord CDATA #IMPLIED>
<!ATTLIST CompanyCompose ToRecord CDATA #IMPLIED>
<!ATTLIST CompanyCompose MaxRecords CDATA #IMPLIED>
<!ATTLIST CompanyCompose ID (0 | 1) "0">
<!ATTLIST CompanyCompose VATNo (0 | 1) "0">
<!-- Svar från CompanyCompose -->
<!ELEMENT CompanyResult (CompanyResultCount,
CompanyResultColumns?,
CompanyResultRecord*)>
<!ATTLIST CompanyResult ReturnCode (OK|ERROR) "OK">
<!ATTLIST CompanyResult ErrorText CDATA #IMPLIED>
<!ATTLIST CompanyResult DeliveryId CDATA #REQUIRED>
<!ATTLIST CompanyResult FileName CDATA #REQUIRED>
<!ATTLIST CompanyResult CustomerId CDATA #REQUIRED>
<!-- Räknar antal poster -->
<!ELEMENT CompanyResultCount EMPTY>
<!ATTLIST CompanyResultCount Total CDATA #REQUIRED>
<!ATTLIST CompanyResultCount FromRecord CDATA #REQUIRED>
<!ATTLIST CompanyResultCount ToRecord CDATA #REQUIRED>
<!ATTLIST CompanyResultCount WithTelephone CDATA #REQUIRED>
<!ATTLIST CompanyResultCount WithTelefax CDATA #REQUIRED>
<!-- Kolumner i utfilen -->
<!ELEMENT CompanyResultColumns EMPTY>
<!ATTLIST CompanyResultColumns ID (0 | 1) "0">
<!ATTLIST CompanyResultColumns VATNo (0 | 1) "0">
<!-- Resultatrecord -->
<!ELEMENT CompanyResultRecord (ID?,
VATNo?)>
then generating corresponding classes.
The problem I'm having now is that the generated classes seems to have the
CompanyQuery as a subclass to CGDocument and all the others as subclasses to
CGNode. However, my plan was to also have the CompanyResult as a subclass to
CGDocument, but I can't understand what I'm doing wrong. Please note that
the DTD is not present completely, because it is huge. Thus it may not
possible to parse it directly.
import java.io.File;
import java.net.URL;
import oracle.xml.parser.v2.DOMParser;
import oracle.xml.parser.v2.DTD;
import oracle.xml.parser.v2.XMLDocument;
import oracle.xml.parser.XMLParser;
import oracle.xml.classgen.*;
public class SampleMain2
{
public SampleMain2()
{
}
public static void main (String args[])
{
// PostCompanyQueryToTheca();
CreateReturnMetaData();
}
public static void parseDTD ()
{
dtdFile = "MyDtd.dtd";
String rootName = "CompanyQuery";
try
{
// Instantiate the parser
DOMParser parser = new DOMParser();
//XMLParser parser = new oracle.xml.parser.v2.XMLParser();
XMLDocument doc = null;
oracle.xml.parser.v2.DTD dtd_v2 = null;
oracle.xml.parser.DTD dtd_v1 = null;
oracle.xml.parser.XMLParser xmlParser = new XMLParser();
xmlParser.setErrorStream(System.out);
xmlParser.setValidationMode(false);
//xmlParser.parseDTD(fileToURL(dtdFile), rootName);
dtd_v2 = new oracle.xml.parser.v2.DTD();
dtd_v1 = (oracle.xml.parser.DTD) xmlParser.getDoctype();
parser.setDebugMode(true);
parser.setErrorStream(System.out);
//parser.setSchemaValidationMode(false);
//parser.setPreserveWhitespace(false);
//parser.setValidationMode(false);
parser.parseDTD(fileToURL(dtdFile), rootName);
dtd_v2 = (DTD)parser.getDoctype();
String doctype_name = null;
doctype_name = rootName;
// generate the Java files...
ClassGenerator generator = new ClassGenerator();
// set generate comments to true
generator.setGenerateComments(true);
// set output directory
generator.setOutputDirectory(".\\src\\DTDClasses\\");
//generator.setOutputDirectory(".\\src\\");
// set validating mode to true
generator.setValidationMode(true);
// generate java src
generator.generate(dtd_v2, doctype_name);
}
catch (Exception e)
{
System.out.println ("XML Class Generator: Error " + e.toString());
e.printStackTrace();
}
}
static public URL fileToURL(String sfile)
{
File file = new File(sfile);
String path = file.getAbsolutePath();
String fSep = System.getProperty("file.separator");
if (fSep != null && fSep.length() == 1)
path = path.replace(fSep.charAt(0), '/');
if (path.length() > 0 && path.charAt(0) != '/')
path = '/' + path;
try
{
return new URL("file", null, path);
}
catch (java.net.MalformedURLException e)
{
// According to the spec this could only happen if the file
// protocol were not recognized.
throw new Error("unexpected MalformedURLException");
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- Fråga mot företag -->
<!ELEMENT CompanyQuery EMPTY>
<!ATTLIST CompanyQuery CompEmployee CDATA #IMPLIED>
<!ATTLIST CompanyQuery Employee CDATA #IMPLIED>
<!-- Anrop till sökmotor, slår samman frågor -->
<!ELEMENT CompanyCompose (CompanyQuery+)>
<!ATTLIST CompanyCompose CustomerId CDATA #REQUIRED>
<!ATTLIST CompanyCompose Duplicates (YES |
VATNO |
CFARNR |
NAME |
NAMELOCALITY |
NAMEADDRESS |
TELEPHONE)
"YES">
<!ATTLIST CompanyCompose SendRecords (NO|DIRECT|FILE)
"NO">
<!ATTLIST CompanyCompose FromRecord CDATA #IMPLIED>
<!ATTLIST CompanyCompose ToRecord CDATA #IMPLIED>
<!ATTLIST CompanyCompose MaxRecords CDATA #IMPLIED>
<!ATTLIST CompanyCompose ID (0 | 1) "0">
<!ATTLIST CompanyCompose VATNo (0 | 1) "0">
<!-- Svar från CompanyCompose -->
<!ELEMENT CompanyResult (CompanyResultCount,
CompanyResultColumns?,
CompanyResultRecord*)>
<!ATTLIST CompanyResult ReturnCode (OK|ERROR) "OK">
<!ATTLIST CompanyResult ErrorText CDATA #IMPLIED>
<!ATTLIST CompanyResult DeliveryId CDATA #REQUIRED>
<!ATTLIST CompanyResult FileName CDATA #REQUIRED>
<!ATTLIST CompanyResult CustomerId CDATA #REQUIRED>
<!-- Räknar antal poster -->
<!ELEMENT CompanyResultCount EMPTY>
<!ATTLIST CompanyResultCount Total CDATA #REQUIRED>
<!ATTLIST CompanyResultCount FromRecord CDATA #REQUIRED>
<!ATTLIST CompanyResultCount ToRecord CDATA #REQUIRED>
<!ATTLIST CompanyResultCount WithTelephone CDATA #REQUIRED>
<!ATTLIST CompanyResultCount WithTelefax CDATA #REQUIRED>
<!-- Kolumner i utfilen -->
<!ELEMENT CompanyResultColumns EMPTY>
<!ATTLIST CompanyResultColumns ID (0 | 1) "0">
<!ATTLIST CompanyResultColumns VATNo (0 | 1) "0">
<!-- Resultatrecord -->
<!ELEMENT CompanyResultRecord (ID?,
VATNo?)>