473,396 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Is it possible to insert the "in line DTD code", in the xml file output, created by a java program?

Hi All,
I wrote a java program which outputs a xml file!
But I would prefer this program to insert the DTD code on the fly, in
the xml file when created!
I want the xml file to look like this:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE students[
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT ssn (#PCDATA)>
<!ELEMENT student (ssn, firstname)>
<!ELEMENT students (student+)>
]>

<students>
<student>
<ssn>444111110</ssn>
<firstname>Jacob</firstname>
</student>
</students>
BUT not like this below:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE students SYSTEM "StudentsDTDfile.dtd">

<students>
<student>
<ssn>444111110</ssn>
<firstname>Jacob</firstname>
</student>
</students>


I used the following code, but it's not working. Doesn't anyone know
what I am doing wrong?

DOMImplementation impl = docBuilderxml.getDOMImplementation();

DocumentType svgDOCTYPE = impl.createDocumentType(
"students", "",
"<!DOCTYPE students [
<!ELEMENT student (ssn, firstname)>
<!ELEMENT ssn (#PCDATA)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT students (student+)>]>");

org.w3c.dom.Document doc = impl.createDocument(null, "students",
svgDOCTYPE);

Apr 23 '07 #1
3 4237
Begreen <Je*************@yahoo.comwrites:
Hi All,
I wrote a java program which outputs a xml file!
But I would prefer this program to insert the DTD code on the fly, in
the xml file when created!
I want the xml file to look like this:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE students[
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT ssn (#PCDATA)>
<!ELEMENT student (ssn, firstname)>
<!ELEMENT students (student+)>
]>

<students>
<student>
<ssn>444111110</ssn>
<firstname>Jacob</firstname>
</student>
</students>
BUT not like this below:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE students SYSTEM "StudentsDTDfile.dtd">

<students>
<student>
<ssn>444111110</ssn>
<firstname>Jacob</firstname>
</student>
</students>


I used the following code, but it's not working. Doesn't anyone know
what I am doing wrong?

DOMImplementation impl = docBuilderxml.getDOMImplementation();

DocumentType svgDOCTYPE = impl.createDocumentType(
"students", "",
"<!DOCTYPE students [
<!ELEMENT student (ssn, firstname)>
<!ELEMENT ssn (#PCDATA)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT students (student+)>]>");

org.w3c.dom.Document doc = impl.createDocument(null, "students",
svgDOCTYPE);
I haven't done what you are trying to do, but I can tell you that
DOMImplementation.createDocumentType() takes a systemId not the text
of a DTD.

The DocumentType object simply refers to a DTD, it doesn't force you
to conform to it when you construct your DOM, so if you just want the
DTD to be able to write it out again, if you have the DTD in a string
anyway, you could output the string when you serialize your DOM, and
skip creating the DocumentType object. For example:

String dtd = "<!DOCTYPE students [ ... ]>";

DOMSource ds = new DOMSource();
Document doc = (Document) ds.getNode();
....
// create your DOM

pw.println("<?xml version='1.0' encoding='UTF-8'?>");
pw.println(dtd);
StreamResult sr = new StreamResult(pw);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
trans.setOutputProperty("omit-xml-declaration", "true");
trans.transform(ds, sr);

Apr 23 '07 #2
Frank Fredstone <no**@not.nowrites:
Begreen <Je*************@yahoo.comwrites:
>Hi All,
I wrote a java program which outputs a xml file!
But I would prefer this program to insert the DTD code on the fly, in
the xml file when created!
I want the xml file to look like this:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE students[
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT ssn (#PCDATA)>
<!ELEMENT student (ssn, firstname)>
<!ELEMENT students (student+)>
]>

<students>
<student>
<ssn>444111110</ssn>
<firstname>Jacob</firstname>
</student>
</students>
BUT not like this below:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE students SYSTEM "StudentsDTDfile.dtd">

<students>
<student>
<ssn>444111110</ssn>
<firstname>Jacob</firstname>
</student>
</students>


I used the following code, but it's not working. Doesn't anyone know
what I am doing wrong?

DOMImplementation impl = docBuilderxml.getDOMImplementation();

DocumentType svgDOCTYPE = impl.createDocumentType(
"students", "",
"<!DOCTYPE students [
<!ELEMENT student (ssn, firstname)>
<!ELEMENT ssn (#PCDATA)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT students (student+)>]>");

org.w3c.dom.Document doc = impl.createDocument(null, "students",
svgDOCTYPE);

I haven't done what you are trying to do, but I can tell you that
DOMImplementation.createDocumentType() takes a systemId not the text
of a DTD.

The DocumentType object simply refers to a DTD, it doesn't force you
to conform to it when you construct your DOM, so if you just want the
DTD to be able to write it out again, if you have the DTD in a string
anyway, you could output the string when you serialize your DOM, and
skip creating the DocumentType object. For example:

String dtd = "<!DOCTYPE students [ ... ]>";

DOMSource ds = new DOMSource();
Document doc = (Document) ds.getNode();
...
// create your DOM

pw.println("<?xml version='1.0' encoding='UTF-8'?>");
pw.println(dtd);
StreamResult sr = new StreamResult(pw);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
trans.setOutputProperty("omit-xml-declaration", "true");
Oops, also:

trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
trans.transform(ds, sr);
Franks
Apr 23 '07 #3
On Apr 22, 10:41 pm, Frank Fredstone <n...@not.nowrote:
Begreen <JenniferChen...@yahoo.comwrites:
Hi All,
I wrote a java program which outputs a xml file!
But I would prefer this program to insert the DTD code on the fly, in
the xml file when created!
I want the xml file to look like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE students[
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT ssn (#PCDATA)>
<!ELEMENT student (ssn, firstname)>
<!ELEMENT students (student+)>
]>
<students>
<student>
<ssn>444111110</ssn>
<firstname>Jacob</firstname>
</student>
</students>
BUT not like this below:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE students SYSTEM "StudentsDTDfile.dtd">
<students>
<student>
<ssn>444111110</ssn>
<firstname>Jacob</firstname>
</student>
</students>
I used the following code, but it's not working. Doesn't anyone know
what I am doing wrong?
DOMImplementation impl = docBuilderxml.getDOMImplementation();
DocumentType svgDOCTYPE = impl.createDocumentType(
"students", "",
"<!DOCTYPE students [
<!ELEMENT student (ssn, firstname)>
<!ELEMENT ssn (#PCDATA)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT students (student+)>]>");
org.w3c.dom.Document doc = impl.createDocument(null, "students",
svgDOCTYPE);

I haven't done what you are trying to do, but I can tell you that
DOMImplementation.createDocumentType() takes a systemId not the text
of a DTD.

The DocumentType object simply refers to a DTD, it doesn't force you
to conform to it when you construct your DOM, so if you just want the
DTD to be able to write it out again, if you have the DTD in a string
anyway, you could output the string when you serialize your DOM, and
skip creating the DocumentType object. For example:

String dtd = "<!DOCTYPE students [ ... ]>";

DOMSource ds = new DOMSource();
Document doc = (Document) ds.getNode();
...
// create your DOM

pw.println("<?xml version='1.0' encoding='UTF-8'?>");
pw.println(dtd);
StreamResult sr = new StreamResult(pw);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
trans.setOutputProperty("omit-xml-declaration", "true");
trans.transform(ds, sr);- Hide quoted text -

- Show quoted text -
Franks,

Thanks, for the response, but where do you use your doc object?

Apr 24 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ashwini Kumar | last post by:
Hi, I want to know if there is a way to get system level information using Java. I am mostly interested in things like Ethernet ID and serial number of Hard disk. Some of the values when you...
5
by: Robert Oschler | last post by:
Preamble: - I know this is the Python forum - I know about (and have used) Jython I already posted this question in comp.lang.java. But after a week I have still not received a single reply....
27
by: Tom Rodman | last post by:
I want to embed a bash script inside the c program. How could you do the below bash snippet in "c"?: cat > /tmp/foo <<\__EOD_ all kinds of nasty stuff in here including single and double...
2
by: Bilal | last post by:
Hello, I'm stuck on this problem for quite some time and hope somebody would be able to guide me. Basically, I need to populate a large number of "template" XML files which have all...
2
dmjpro
by: dmjpro | last post by:
suppose i open a file in write mode. if the file exists then the new content will be overwritten and the file opened in append mode then the exsiting file content will be appended. can't i do...
11
by: Grzesiek | last post by:
Hi I have Main.cpp and Hello.cpp files in my Dev C++ project. Main.cpp #include "Hello.cpp" int main(){ return 0;
3
by: nkoriginal | last post by:
Hi: I want to create an store procedure for an ASP page. I've a form with 100 fields and I want to insert the information in: 1) one table with 100 rows or 2) in 5 tables Now, my idea is...
4
by: =?utf-8?B?Qm9yaXMgRHXFoWVr?= | last post by:
Hello, (sorry to begin with Java in a Python list ;-) in Java, when I want to pass input to a function, I pass "InputStream", which is a base class of any input stream. In Python, I found...
1
by: simon_s_li | last post by:
Hi, Does anyone have any coding example on how to select a text or csv file (file open diagloue box) from my local drive and then read it in the backend using some kind of inputsteam? Thanks...
3
by: Tuy Solang | last post by:
Hi Everyone, I am just starting to learn Java from O'Reilly CD. I could not find any example that is related to screen capture. Is it possible in Java like in Microsoft C? Thanks,
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.