Home Answers Viewqa XML How to parse an XML as per dtd in java

 
 


Naveen Saini
How to parse an XML as per dtd in java
3 Answer(s)      a year and 5 months ago
Posted in : XML

Please help to tell me a logic to parse an xml as per mentioned dtd inside xml. I've used SAX and DOM both but that only check for well formed xml tags but don't validate as per DTD. please help

View Answers

December 26, 2011 at 4:28 PM



December 26, 2011 at 7:04 PM


Many Thanks for your help, I've changed little in code and found minimized code for any dtd mentioned in xml. please advice for my below commented lines.

public boolean ParseXML(String filename)

        {
String DTD_PATH = c:/dtd/;

          valid = true;  
          try{
              DocumentBuilderFactory factory = 
              DocumentBuilderFactory.newInstance();
              factory.setValidating(true);
              DocumentBuilder builder = factory.newDocumentBuilder();
                  builder.setErrorHandler(new org.xml.sax.ErrorHandler() 
                  {
                  //Ignore the fatal errors
                      public void fatalError(SAXParseException exception)
                       throws SAXException { }
                      //Validation errors 
                      public void error(SAXParseException e)
                          throws SAXParseException {
                          System.out.println("Error at " +e.getLineNumber() + " line.");
                          m.log.info(this,"PARSING ERROR at line : "+e.getColumnNumber()+" : "+e.getMessage());
                          System.out.println(e.getMessage());
                          valid = false;
                          }
                      //Show warnings
                      public void warning(SAXParseException err)
                      throws SAXParseException{
                        m.log.info(this,"PARSING ERROR at line : "+err.getColumnNumber()+" : "+err.getMessage());
                      System.out.println(err.getMessage());
                      valid = false;
                      //System.exit(0);
                      }
                  });                 
              Document xmlDocument = builder.parse(new FileInputStream(filename),DTD_PATH);

//Please tell me the use of below commented lines since now this code can run on any mentioned dtd in xml.            
/*DOMSource source = new DOMSource(xmlDocument);                          
              StreamResult result = new StreamResult(System.out);
              TransformerFactory tf = TransformerFactory.newInstance();
              Transformer transformer = tf.newTransformer();
              transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "c:/dtd/EMC-UPDATE.dtd");
              transformer.transform(source, result);              */

              }
              catch (Exception e) {
              System.out.println("ERROR :: "+e.getMessage());
              m.log.info(this,"ERROR : "+m.ctool.getException(e));
              return false;
              }
              return valid;
          }

December 26, 2011 at 7:05 PM


Many Thanks for your help, I've changed little in code and found minimized code for any dtd mentioned in xml. please advice for my below commented lines.

public boolean ParseXML(String filename)

        {
String DTD_PATH = c:/dtd/;

          valid = true;  
          try{
              DocumentBuilderFactory factory = 
              DocumentBuilderFactory.newInstance();
              factory.setValidating(true);
              DocumentBuilder builder = factory.newDocumentBuilder();
                  builder.setErrorHandler(new org.xml.sax.ErrorHandler() 
                  {
                  //Ignore the fatal errors
                      public void fatalError(SAXParseException exception)
                       throws SAXException { }
                      //Validation errors 
                      public void error(SAXParseException e)
                          throws SAXParseException {
                          System.out.println("Error at " +e.getLineNumber() + " line.");
                          m.log.info(this,"PARSING ERROR at line : "+e.getColumnNumber()+" : "+e.getMessage());
                          System.out.println(e.getMessage());
                          valid = false;
                          }
                      //Show warnings
                      public void warning(SAXParseException err)
                      throws SAXParseException{
                        m.log.info(this,"PARSING ERROR at line : "+err.getColumnNumber()+" : "+err.getMessage());
                      System.out.println(err.getMessage());
                      valid = false;
                      //System.exit(0);
                      }
                  });                 
              Document xmlDocument = builder.parse(new FileInputStream(filename),DTD_PATH);

//Please tell me the use of below commented lines since now this code can run on any mentioned dtd in xml.            
/*DOMSource source = new DOMSource(xmlDocument);                          
              StreamResult result = new StreamResult(System.out);
              TransformerFactory tf = TransformerFactory.newInstance();
              Transformer transformer = tf.newTransformer();
              transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "c:/dtd/EMC-UPDATE.dtd");
              transformer.transform(source, result);              */

              }
              catch (Exception e) {
              System.out.println("ERROR :: "+e.getMessage());
              m.log.info(this,"ERROR : "+m.ctool.getException(e));
              return false;
              }
              return valid;
          }









Related Pages:
How to parse an XML as per dtd in java
How to parse an XML as per dtd in java  Please help to tell me a logic to parse an xml as per mentioned dtd inside xml. I've used SAX and DOM both but that only check for well formed xml tags but don't validate as per DTD. please
XML Validate DTD
. Description of program: Validating a XML file against a DTD needs a xml file and its DTD document. First of all construct a well-formed xml file along with a DTD file . This DTD file defines all elements to keep in the xml file. After
DTD
DTD   hello, what is DTD? define DTD.   hii, DTD stands for Document Type definition. DTD is a just like a rule book for an XML documents. Once DTD is ready, you can create number of XML documents which will follow
parse xml
; </STATE> How to parse the above xml. please help me   Here is a code that parses an xml file using DOM Parser. import org.w3c.dom.*; import...parse xml   <STATES> <STATE sname="AndhraPradesh" >
Problem with external DTD - XML
Problem with external DTD  Hi, This class generate an XML file.But while I am running that generated xml file its giving error as can not locate... classes. OutputFormat of = new OutputFormat("XML","ISO-8859-1",true); of.setIndent
Java code to parse xml - Java Beginners
/GetData.shtml The example explains how to parse a xml document. Thanks...Java code to parse xml   TL1 10.50.26.98... to parse the code and get the values from
XML DTD
XML DTD  What is DTD in xml file ?   A DTD (Document Type Definition) defines the structure and building blocks of a xml document. It can be also written inside the xml document
how to parse xml in j2me tutorial
how to parse xml in j2me tutorial   i want to parse xml file in j2me wireless toolkit.i saw ur examples & source code for parsing xml file in j2me & i try to execute same example on my pc i some how manage to run
Introduction to DTD's
Introduction to DTD's To validate an xml document there should be some set... and attributes. DTD specifies that an xml can contain what elements... with XML document DTD appears near the start of the xml document. Declaration
how to parse xml document which contains elements with namespace using JDOM - XML
is inside so how can i parse this xml using JDOM,java. Thanks Shree  Hi...how to parse xml document which contains elements with namespace using JDOM  Hello, I am parsing XML file ,which has structure like
Creating a DTD
definition (DTD) is used to validate XML files that reference the DTD file... etc for any XML file and defines constraints for how each element, attribute etc may be used within of any XML files that reference the DTD file. DTDs
XML Schema
: #FFFFFF; } XML Schema An XML Schema defines how to structure an XML document and it can be used in place of DTD. XML Schema is based on XML. XML Schema... schema 12 Xml Parser can be used to parse the schema file 13
DOM Parser Example to Parse A DOM Document
DOM Parser Example to Parse A DOM Document In this tutorial, we will discuss about how to parse(retrieve data) DOM Document. The XML DOM views an XML... the XML Code Download the JAVA code/a>
php parse soap response
php parse soap response  What is soap and how can i convert a soap response to XML. Thanks in Advance
How to parse the attributes from XML using KXML parser in j2me
How to parse the attributes from XML using KXML parser in j2me  Dear Sir, I have doubt in How to parse the attributes from XML using KXML parser in j2me ..I parse the dge name value..but i didn't do parsing the attributes
Java - How to read a string line per line
Java - How to read a string line per line  Java - How to read a string line per line I am trying to find the tutorial for reading the String line... to read the big String line per line? Is there any API available in Java
DTD - XML Constituent
DTD - XML Constituent       The constituent components of XML DTD Documents.  DTDs are made up... constituent components of both XML documents. Elements can contain text, other
Processing XML with Java
(parse, validate, transform and query) xml documents in java based applications...; } Processing XML with Java XML is cross-platform software, hardware... and not the replacement. Sample XML document Below is sample xml file. You can see how
How to parse the attributes values from XML using KXML parser in j2me
How to parse the attributes values from XML using KXML parser in j2me  hai team, i did xml parsing from serverside...i got a resultant value from xml,but here tag value name only came from parsing attribute name cant come
Could not parse configuration: hibernate.cfg.xml
Could not parse configuration: hibernate.cfg.xml  Hi, I am getting.... Exception in thread "main" org.hibernate.HibernateException: Could not parse...\empdetails\hibernate-configuration-3.0.dtd (The system cannot find the file specified
how do you parse by reference in java(with JGrasp)
how do you parse by reference in java(with JGrasp)  i am a 1st year beginner in java programming(GR 10) at hillcrest High School in south Africa My question is how do you parse by reference in java(with JGrasp) please help me i
how to parse a csv file using standard libraries?
how to parse a csv file using standard libraries?  hie i am a beginner in java i want to parse a csv file using any standard libraries i want to know how the libraries are imported and used in eclipse thanks in advance
Use of <x:parse> tag of JSTL
;    In this section we will learn how to use <x:parse> tag of  Xml tag library of Jstl. This tag is used to parse the specified xml document. Standard Syntax : <%@ taglib prefix="x" uri="
DTD-Attributes
DTD-Attributes       In a DTD, attributes are declared with an ATTLIST declaration. Declaring...; DTD example: <!ATTLIST reciept type
java - XML
java  how can i validate my xml file using java code plz send me de... kumar  Hi friend, Step to validate a xml file against a DTD (Document Type Definition) using the DOM APIs. Here is the xml file "User.xml
XML validation against a dtd -refering http://www.w3.org/1999/xmlschema
XML validation against a dtd -refering  hi i have an issue in validating an xml against dtd .As the currently used schema is http..." old version is there any solution to validate my xml against the dtd i have
DTD-Elements
DTD-Elements       In a DTD, elements are declared with an ELEMENT declaration. Declaring Elements : syntax In a DTD, XML elements are declared  with the following syntax
XML and swings - Java Beginners
XML and swings  I have an xml file where all the contents that should be present in the tabbed pane are there. i would like to know how to parse the xml such that, whenever it reads the component type as label, it should create
Java XML Parsing Using SAX
Java XML Parsing Using SAX To Parse XML document using SAX parser method you... = saxParserFactory.newSAXParser(); // Parsing XML Document by calling parse method... = saxParserFactory.newSAXParser(); // Parsing XML Document by calling parse method of SAXParser
Java XML Parsing Using DOM
Java XML Parsing Using SAX To Parse XML document using SAX parser method you... = saxParserFactory.newSAXParser(); // Parsing XML Document by calling parse method... = saxParserFactory.newSAXParser(); // Parsing XML Document by calling parse method of SAXParser
Validating XML document with a DTD
Validating XML document with a DTD If an xml document is well formed i.e.... an XML document valid it must be validated, or verified, against a DTD. DTD... in dtd. If your xml file defines the dtd name to ensure the validity of the file
XML - XML
XML  What is specific definatio of XML? Tell us about marits and demarits of XML? use of XML? How we can use of XML?  Hi, XML... Definition (DTD) , XML Schema to describe the data. Hence it is self-descriptive too
xml
xml  how to creatte html file and validate using java and finally i need get web.xml file
XML Tutorials
.    XML:Validation How a DTD is used... you how to Load Properties from the XML file via a DOM document. JAXP (Java... shows you how to Transform XML with the XSLT in a DOM document. JAXP (Java API
xml - XML
xml  define parser and xml,in jaxp api how to up load parsers... it into identifiable chunks before translation. If the parser cannot parse a program then the program contains syntax errors. XML is defined as an extensible
xml - XML
xml  hi convert xml document to xml string.i am using below code... = documentBuilderFactory.newDocumentBuilder().parse(inputStream); StringWriter stw = new... = stw.toString(); after i am getting xml string result, like Successxxx profile
xml - XML
xml  how to match to xml file?can you give example with java code
DTD-Entities
DTD-Entities      ... <!ENTITY entity-name "entity-value"> DTD... company "RoseIndia"> XML example: <
HOW TO PARSE FILE WITH SEVERAL DELIMITERS AND LINE FEEDERS - Java Beginners
HOW TO PARSE FILE WITH SEVERAL DELIMITERS AND LINE FEEDERS  Hi All i need to parse a file of this format,such that i can get/fetch the values present between the tags by avoiding linefeeds or "/n",carriage return 0r "/r
to retrive e mails as per user name - Java Beginners
to retrive e mails as per user name  hi friends, how to retrive e mails as per user "user name " for ex: class Mail{ private String subject.../reading-message-using-java-mail.shtml Thanks
Need to Process 10 million XML in a second
you please advice me how to do that in Java. Please advice me how to parse... million XML's per day and i need to do the below process within a minute by using java . The XML should be parsed (Any Parser DOM etc...). Need to do some
parse map in java
parse map in java  What is a Parser? Can you give the source code to parse map in Java
Retrieving Data From the XML file
towards showing how to construct a Java object from an XML document. ...;    In this example we will examine how to parse...:\apache-tomat-5.5.23\bin\. Now we need to parse method to actually parse the XML file
xml - XML
xml  hi convert xml file to xml string in java ,after getting the xml string result and how to load xml string result? this my problem pls help..."); FileOutputStream output = new FileOutputStream("new.xml"); ReadXML xml = new
XML and JavaScript
JavaScript and XML In this section we will see how JavaScript and XML... libraries to fetch the server data. Developer can use these XML data or JSON data retrieved from the server. Here XML plays important role data transfer
creating Xml - Java Beginners
creating Xml  how we can create a xml file using java Creating XML File is possible in java with this packages. import... this Elemnts to DOcumnet object. Creating XML File is possible  Creating XML File is possible
php parse string into array
php parse string into array  How can i parse a string into array and convert an array into string
php parse ini string
php parse ini string  Need to know how to parse the ini file to extract values
parse url iphone sdk
parse url iphone sdk  How can i parse two different URL's using NSXMLParser
java and xml - XML
java and xml  Hi Deepak, I want learn xml and java.... (myeclipse--how it is help to me?) Thank You. Ramesh Reddy T.  Hi friend, http://www.roseindia.net/xml/dom/ Thanks

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.