View RSS Feed

Java XML

  1. Java SAX Schema Validation

    by , 04-27-2012 at 06:16 PM
    It is possible to turn on XML Schema validation during parsing with a SAXParser. Here is how it looks:
    Java Code:
    Schema schema = null;
    try {
      String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
      SchemaFactory factory = SchemaFactory.newInstance(language);
      schema = factory.newSchema(new File(name));
    } catch (Exception e) {
        e.printStackStrace();
    }
    
    
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setSchema(schema);
    ...
    Tags: sax schema Add / Edit Tags
    Categories
    SAX.
  2. Java SAX DefaultHandler Exceptions

    by , 04-27-2012 at 06:16 PM
    The DefaultHandler class has three methods you can override to handle exceptions encountered during the XML parsing. Here they are:
    Java Code:
    public void warning(SAXParseException e) throws SAXException {
    }
    
    public void error(SAXParseException e) throws SAXException {
    }
    
    public void fatalError(SAXParseException e) throws SAXException {
    }
    Let's say that the parser encounters an illegal XML entity (like &notLegal;). The SAXParser ...
    Categories
    SAX.
  3. Java SAX Parser

    by , 04-27-2012 at 06:15 PM
    SAX is an abbreviation and means "Simple API for XML". A Java SAX XML parser is a stream oriented XML parser. It works by iterating over the XML and call certain methods on a "listener" object when it meets certain structural elements of the XML. For instance, it will call the listener object for the following "events":
    - startDocument
    - startElement
    - characters
    - comments
    - processing instructions
    - endElement
    - endDocument ...
    Categories
    SAX.
  4. SAX for Survival

    by , 04-27-2012 at 06:14 PM
    Compared to the DOM API, the SAX API is an attractive approach. SAX doesn't have a generic object model, so it doesn't have the memory or performance problems associated with abusing thenew operator. And with SAX, there is no generic object model to ignore if you plan to use a specific problem-domain object model instead. Moreover, since SAX processes the XML document in a single pass, it requires much less processing time.
    SAX does have a few drawbacks, but they are mostly related to the ...
    Categories
    SAX.
  5. SAX and DOM APIs

    by , 04-27-2012 at 06:14 PM
    The SAX API is event-based. XML parsers that implement the SAX API generate events that correspond to different features found in the parsed XML document. By responding to this stream of SAX events in Java code, you can write programs driven by XML-based data.

    The DOM API is an object-model-based API. XML parsers that implement DOM create a generic object model in memory that represents the contents of the XML document. Once the XML parser has completed parsing, the memory contains ...
    Tags: sax Add / Edit Tags
    Categories
    SAX.
  6. Dangers of DOM

    by , 04-27-2012 at 06:13 PM
    At first glance, the DOM API seems to be more feature-rich, and therefore better, than the SAX API. However, DOM has serious efficiency problems that can hurt performance-sensitive applications.

    The current group of XML parsers that support DOM implement the in-memory object model by creating many tiny objects that represent DOM nodes containing either text or other DOM nodes. This sounds natural enough, but has negative performance implications. One of the most expensive operations ...
    Categories
    DOM
  7. Java DOM Schema Validation

    by , 04-27-2012 at 06:12 PM
    In Java it is possible to validate a DOM graph according to an XML Schema. The technique I will show here is validation after the DOM graph is created, not during creation as was otherwise the norm with DTD validation.
    First you must load the XML Schema into a Schema object. Once loaded into a Schema object you can use that same Schema object to validate multiple DOM graphs. This is smart, because then you only have to parse the XML Schema once. The result is significantly better performance, ...
    Tags: dom schema Add / Edit Tags
    Categories
    DOM
  8. DOM Element Attributes

    by , 04-27-2012 at 06:12 PM
    As you have already seen, you can access the attributes of an element via the Element interface. There are two ways to do so:
    Java Code:
    String attrValue = element.getAttribute("attrName");
    Attr attribute = element.getAttributeNode("attrName");
    Most of the time the getAttribute() method will do just fine.
    The Attr interface extends Node. It allows you to access the owning element via the methodgetOwnerElement() etc. Accessing an attribute via ...
    Categories
    DOM
  9. DOM Elements, Child Elements, and the Node Interface

    by , 04-27-2012 at 06:11 PM
    The root element has children which can be elements, comments, processing instructions, characters etc. You get the children of an element like this:
    Java Code:
    NodeList nodes = element.getChildNodes();
    
    for(int i=0; i<nodes.getLength(); i++){
      Node node = nodes.item(i);
    
      if(node instanceof Element){
        //a child element to process
        Element child = (Element) node;
        String attribute = child.getAttribute("width");
    ...
    Tags: dom elements Add / Edit Tags
    Categories
    DOM
  10. Creating DOM Child Elements

    by , 04-27-2012 at 06:10 PM
    This lesson shows you how to create root and child elements in the DOM tree. We will first create a blank DOM document and add the root element to it. Then I show you how to add comment element and then child element to the root element. In this example following xml code will generated and displayed on the console.
    XML Code:
    <?xml version="1.0" encoding="UTF-8" ?> 
    <root>
    <!-- This is comment--> 
    <Child attribute1="The value
    ...
    Categories
    DOM
  11. Creating Blank DOM Document

    by , 04-27-2012 at 06:09 PM
    This tutorial shows you how to create blank DOM document. JAXP (Java API for XML Processing) is a Java interface that provides a standard approach to Parsing XML documents. With JAXP, we will use the Document BuilderFactory to create DocumentBuilder class.
    The class DocumentBuilderFactory is responsible for creating new DOM parsers. Normally it is used to a DOM parser. Example is as follows:
    Java Code:
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
     DocumentBuilder
    ...
    Categories
    DOM
  12. What is DOM?

    by , 04-27-2012 at 06:08 PM
    Document Object Model: DOM is a platform- and language-neutral interface, that provides a standard model of how the objects in an XML object are put together, and a standard interface for accessing and manipulating these objects and their inter-relationships.

    The DOM is an interface that exposes an XML document as a tree structure comprised of nodes. The DOM allows you to programmatically navigate the tree and add, change and delete any of its elements.

    The DOM programming ...
    Tags: what is dom? Add / Edit Tags
    Categories
    DOM
  13. An Overview of the Packages

    by , 04-27-2012 at 06:08 PM
    The SAX and DOM APIs are defined by the XML-DEV group and by the W3C, respectively. The libraries that define those APIs are as follows:

    • javax.xml.parsers: The JAXP APIs, which provide a common interface for different vendors' SAX and DOM parsers
    • org.w3c.dom: Defines the Document class (a DOM) as well as classes for all the components of a DOM
    • org.xml.sax: Defines the basic SAX APIs
    • javax.xml.transform: Defines the XSLT APIs that let you transform XML into other forms
    ...
    Categories
    JAXP
  14. Sun's JAXP and Sun's parser

    by , 04-27-2012 at 06:07 PM
    A lot of the parser/API confusion results from how Sun packages JAXP and the parser that JAXP uses by default. In earlier versions of JAXP, Sun included the JAXP API (with those six classes I just mentioned and a few more used for transformations) and a parser, called Crimson. Crimson was part of the com.sun.xml package. In newer versions of JAXP -- included in the JDK -- Sun has repackaged the Apache Xerces parser . In both cases, though, the parser is part of the JAXP distribution, but not part ...
    Tags: sun's jaxp Add / Edit Tags
    Categories
    JAXP
  15. JAXP: API or abstraction?

    by , 04-27-2012 at 06:06 PM
    Strictly speaking, JAXP is an API, but it is more accurately called an abstraction layer. It doesn't provide a new means of parsing XML, nor does it add to SAX or DOM, or give new functionality to Java and XML handling. (If you're in disbelief at this point, you're reading the right article.) Instead, JAXP makes it easier to use DOM and SAX to deal with some difficult tasks. It also makes it possible to handle some vendor-specific tasks that you might encounter when using the DOM and SAX APIs, in ...
    Categories
    JAXP
  16. The JAXP APIs

    by , 04-27-2012 at 06:05 PM
    The main JAXP APIs are defined in the javax.xml.parsers package. That package contains vendor-neutral factory classes,SAXParserFactory, DocumentBuilderFactory, and TransformerFactory, which give you a SAXParser, aDocumentBuilder, and an XSLT transformer, respectively. DocumentBuilder, in turn, creates a DOM-compliant Documentobject.

    The factory APIs let you plug in an XML implementation offered by another vendor without changing your source code. The implementation you get depends ...
    Categories
    JAXP
  17. Introduction to JAXP

    by , 04-27-2012 at 06:04 PM
    The Java API for XML Processing (JAXP) is for processing XML data using applications written in the Java programming language. JAXP leverages the parser standards Simple API for XML Parsing (SAX) and Document Object Model (DOM) so that you can choose to parse your data as a stream of events or to build an object representation of it. JAXP also supports the Extensible Stylesheet Language Transformations (XSLT) standard, giving you control over the presentation of the data and enabling you to convert ...
    Tags: jaxp Add / Edit Tags
    Categories
    JAXP
  18. The Benefits of JAXP

    by , 11-29-2011 at 02:58 AM
    One of the most important technologies available in java is the APIs used to work with XML. There are basically two ways to work with XML documents. SAX involves an event driven means of processing XML using callbacks to handle the relevant events. DOM involves using an in-house tree structure of the XML document. Sun Microsystems created a Java API for XML Processing (JAXP) toolkit which makes XML manageable for all developers to use. It is a key component for exploiting all the possibilities with ...

    Updated 11-30-2011 at 03:01 PM by Java XML

    Categories
    SAX. , JAXP , DOM