aspose file tools*
The moose likes XML and Related Technologies and the fly likes very basic question --- validating with DTD Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "very basic question --- validating with DTD" Watch "very basic question --- validating with DTD" New topic
Author

very basic question --- validating with DTD

mou haj
Ranch Hand

Joined: Sep 12, 2001
Posts: 81
Im very new to XML...and asking a avery basic question
I have made an XML file
public class TestXML {
/**
* TestXML constructor comment.
*/
public TestXML() {
super();
}
public static void main(String args [])
{

try {
Vector name= new Vector();
Vector age= new Vector();
Vector state= new Vector();
name.addElement("name1");
name.addElement("name2");

age.addElement("age1");
age.addElement("age2");
state.addElement("state1");
state.addElement("state2");
String xmlFile = "C:/XML/test.xml";
FileWriter XMLFileFileWriter = new FileWriter("C:/XML/test.xml");
String prolog = "<?xml version='1.0' encoding='utf-8'?>\n";
XMLFileFileWriter.write(prolog);

String dtdInfo = "<!DOCTYPE userprofile SYSTEM \"http://localhost/estart/profile.dtd\">\n";
XMLFileFileWriter.write(dtdInfo);

XMLFileFileWriter.write("<userprofile>\n");
for(int i=0; i<name.size();i++){
XMLFileFileWriter.write("<name>"+name.elementAt(i)+"</name>\n");
XMLFileFileWriter.write("<age>"+age.elementAt(i)+"</age>\n");
XMLFileFileWriter.write("<state>"+state.elementAt(i)+"</state>\n");
}
XMLFileFileWriter.write("</userprofile>\n");

XMLFileFileWriter.close();
}
catch (Exception e) {
}


}
}

AND the DTD is as follows :
<!ELEMENT profile (userprofile)>
<!ELEMENT userprofile (name,age, state)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT state (#PCDATA)>
I have Xalan Xerces.
Now what should i do to confirm that my XMl file confirms to my dtd.
Please help
Thanx
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Read this tutorial it will help you.
Validating XML against it DTD, link=validating XML


Spritle Software Blogs
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
I think you should use DOM or JDom to write an XML decument in memory, then output to a file. In this way, you can at least to guarantee the well-formness of your file. You can also to turn the validation on.
Write line-by-line, any smart text editor can do better job than you write Java code to do it.
mou haj
Ranch Hand

Joined: Sep 12, 2001
Posts: 81
Thanx balaj
the tutorial is great .. now im trying with schemas...
can you tell me where im going wrong...i have placed another question
mou haj
Ranch Hand

Joined: Sep 12, 2001
Posts: 81
Hi balaji
Im very new to XML...
Can you tell me suppose I have an XML file and I have the XML schema .. how now ill have to extract the data and fill in a database. I mean I know ill have to parsing... but how.. can u gimme some sample code...or refer any good tutorial ...
thanx
Moumita
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Originally posted by mou haj:
Hi balaji
Im very new to XML...
Can you tell me suppose I have an XML file and I have the XML schema .. how now ill have to extract the data and fill in a database. I mean I know ill have to parsing... but how.. can u gimme some sample code...or refer any good tutorial ...
thanx
Moumita

here u go!..


Using JDBC to extract data into XML , Using JDBC to insert data from XML into a database and also this xml Schema validation using XERCES2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: very basic question --- validating with DTD
 
Similar Threads
question on mock exam
validating against a DTD
xml - addAttribute(..) error
Overrinding ATTLIST in DTD
xml, dtd, and parsing code