6 Replies Latest reply: Jan 22, 2010 4:24 AM by gimbal2 RSS

    Read XML Element Value with Whitespaces By SAX

    807580
      Hi All,

      * How to read XML element value with whitespaces by SAX

      * I have tried it by DOM and working well
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                factory.setIgnoringElementContentWhitespace(true);
      * Is it possible by using SAX....?

      * My XML file looks like the following,
      <Employee>
      <Name>  aaa</Name>
      <Age>27   </Age>
      </Employee>
      * Please give me some solution for this

      Thanks and Regards,
      javaimran
        • 1. Re: Read XML Element Value with Whitespaces By SAX
          gimbal2
          your question is a little vague. Do you WANT whitespace, or you do NOT want whitespace?

          You say "with whitespace", but your DOM example shows how to ignore whitespace :s


          If you do not want whitespace, a simple String.trim() should do it.
          • 2. Re: Read XML Element Value with Whitespaces By SAX
            807580
            I want to read XML element value with whitespaces only
            factory.setIgnoringElementContentWhitespace(true);
            setIgnoringElementContentWhitespace(boolean whitespace) 
                      Specifies that the parsers created by this factory must eliminate whitespace in element content (sometimes known loosely as 'ignorable whitespace') when parsing XML documents
            That is when you set argument value as true, then it keeps whitespace of the element or else it will be trimmed. By default it false. so i make it true.

            How to do this same process by using SAX....?

            Thanks
            javaimran
            • 3. Re: Read XML Element Value with Whitespaces By SAX
              gimbal2
              Well I've worked with SAX myself plenty of times, and I never had any problem with it trimming values (in fact I had to do that myself). The XML standard states that parsers should give the value exactly as it is in the document.

              So are you really sure that the whitespace is being trimmed? How do you check that? And how do you use SAX, could you provide a short example of your parsing code?
              • 4. Re: Read XML Element Value with Whitespaces By SAX
                807580
                * I tried with simple SAX example by the following code without schema validation, its working fine(i.e, It keeps the whitespace)

                     
                SAXTransformerFactory transfactory = (SAXTransformerFactory) TransformerFactory.newInstance();
                         Transformer trans = transfactory.newTransformer();
                
                         SAXSource sax_source = new SAXSource(new InputSource(new File(arg[0]).toURI().toString()));
                         DOMResult dom_result = new DOMResult ();
                         trans.transform(sax_source, dom_result); 
                         Document meta_data = (Document) dom_result.getNode();
                         DOMSource dom_source = new DOMSource(meta_data);
                         createDomFile(dom_source, "E:/Temp/output1.xml");
                         System.out.println("SAX file created...");
                * And when i tried with SAX against schema validation by the following code then it removes the whitespace,

                     
                SAXTransformerFactory transfactory = (SAXTransformerFactory) TransformerFactory.newInstance();
                     Transformer trans = transfactory.newTransformer();
                     trans.setErrorListener(XXXX);
                     SAXParserFactory fact = SAXParserFactory.newInstance();
                     fact.setValidating(true);
                     fact.setNamespaceAware(true);
                     SAXParser parser = fact.newSAXParser();
                     parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
                     XMLReader reader = parser.getXMLReader();
                                reader.setContentHandler(XXXX);
                                 -  -   -   
                                 -  -   -   
                     pp.setParent(reader);
                     SAXSource sax_source = new SAXSource(XXXX, new InputSource(file.getAbsoluteFile().toURI().toString()));
                         DOMResult dom_result = new DOMResult ();
                         trans.transform(sax_source, dom_result);
                * Please correct me if am doing anything wrong in the above code, Because while validating SAX file against schema it removes whitespaces.........How to solve it...?
                • 5. Re: Read XML Element Value with Whitespaces By SAX
                  807580
                  * While debuging my code, i found that the following method is removes the whitespaces.......
                  fact.setValidating(true);
                  * Is it necessary to call this method while validating against schema....?
                  • 6. Re: Read XML Element Value with Whitespaces By SAX
                    gimbal2
                    actually setValidating() will validate against a DTD, not a schema. Leave this property as false (the default) and use setSchema() to be able to do schema validation.