This content has been marked as final. Show 6 replies
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.
I want to read XML element value with whitespaces only
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.
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
How to do this same process by using SAX....?
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?
* I tried with simple SAX example by the following code without schema validation, its working fine(i.e, It keeps the whitespace)
* 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(); SAXSource sax_source = new SAXSource(new InputSource(new File(arg).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...");
* 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...?
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);
* While debuging my code, i found that the following method is removes the whitespaces.......
* Is it necessary to call this method while validating against schema....?
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.