Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



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

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Naveen Saini
How to parse an XML as per dtd in java
3 Answer(s)      2 years and 10 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


Advertisement
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 Tutorials/Questions & Answers:
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 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
Advertisements
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
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
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" >
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
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
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
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
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
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
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
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
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
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
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
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>
parse map in java
parse map in java  What is a Parser? Can you give the source code to parse map in Java
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
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
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
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
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
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 get this xml syntx in java??????
how to get this xml syntx in java??????  how to get this xml syntx in java?????? < comp xlink:type="new" xlink:actuate="onload"/>
how to get this xml syntx in java??????
how to get this xml syntx in java??????   how to get this xml syntx in java?????? < comp xlink:type="new" xlink:actuate="onload"/>
In data structure in java how to parse the given string and counts the member of character that match the given data?
In data structure in java how to parse the given string and counts the member of character that match the given data?  Design a parser that parse the given string and counts the member of character that match the given data.using
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
how to write java data - XML
how to write java data  how to write data to xml file  Hi friend, Read for more information, http://www.roseindia.net/xml/dom/ Thanks
how to update xml from java - XML
how to update xml from java  hi, Im new to xml parsing and dont know much about. I need to modify the attribute val of a tag in a complex xml file by using java code tell me the procedure. Thanks in advance.   hi
Introduction to XML - DTD
Introduction to  XML - DTD           Introduction to XML XML is a cross... Type Definition (DTD), XML Schema to describe the data. Hence it is self
 

 

 

DMCA.com