• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

very basic question --- validating with DTD

 
mou haj
Ranch Hand
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3150
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read this tutorial it will help you.
Validating XML against it DTD, link=validating XML
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3150
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic