Whitespace problem - Xerces & Java - Anyone?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John

    #1

    Whitespace problem - Xerces & Java - Anyone?


    I have been trying with no result, to force Xerces parsers to ignore
    whitespace in my XML file. I have tried several of the NG suggested
    setFeature() methods in DOMParser to no avail.

    I have now started to use the newest version of Xerces and as a result
    I stopped using DOMParser. The following fragment of my code shows how
    I create the parser and parse my XML file, which still has <null>
    #text fields for whitespace. How can I force Xerces to ignore them?

    Thanks,
    John

    Java Code:
    =======

    public class XMLConvert {

    private DocumentBuilder Factory builderFactory;
    private DocumentBuilder docFactory;
    private Document inDoc;

    public static void main(String[] args)
    {
    XMLConvert xml = null;

    try {
    xml = new XMLConvert(new FileInputStream (new File(args[0])));
    } catch(Exception e) {
    e.printStackTra ce();
    }

    }

    public XMLConvert(Inpu tStream is)
    {
    try {
    builderFactory = DocumentBuilder Factory.newInst ance();

    docFactory = builderFactory. newDocumentBuil der();

    inDoc = docFactory.pars e(is);
    } catch(Exception e) {
    e.printStackTra ce();
    }
    }

    My simple XML file:
    ============
    <?xml version="1.0" encoding="UTF-8"?>
    <Envelope>
    <Body>
    Test
    </Body>
    </Envelope>

    Output from Java (children of root element):
    =============== =============

    Node Name: #text
    Local Name: null
    URI: null

    Node Name: Body
    Local Name: null
    URI: null

    Node Name: #text
    Local Name: null
    URI: null
  • Philippe Poulard

    #2
    Re: Whitespace problem - Xerces &amp; Java - Anyone?

    Hi,

    this method allow white spaces to be ignored :
    builderFactory. setIgnoringElem entContentWhite space(true);
    but you must specify a grammar (a DTD) for your document (the parser
    can't guess which spaces can be ignored)

    see JAXP documentation :
    http://java.sun.com/j2se/1.4.2/docs/...erFactory.html

    John wrote:[color=blue]
    > I have been trying with no result, to force Xerces parsers to ignore
    > whitespace in my XML file. I have tried several of the NG suggested
    > setFeature() methods in DOMParser to no avail.
    >
    > I have now started to use the newest version of Xerces and as a result
    > I stopped using DOMParser. The following fragment of my code shows how
    > I create the parser and parse my XML file, which still has <null>
    > #text fields for whitespace. How can I force Xerces to ignore them?
    >
    > Thanks,
    > John
    >
    > Java Code:
    > =======
    >
    > public class XMLConvert {
    >
    > private DocumentBuilder Factory builderFactory;
    > private DocumentBuilder docFactory;
    > private Document inDoc;
    >
    > public static void main(String[] args)
    > {
    > XMLConvert xml = null;
    >
    > try {
    > xml = new XMLConvert(new FileInputStream (new File(args[0])));
    > } catch(Exception e) {
    > e.printStackTra ce();
    > }
    >
    > }
    >
    > public XMLConvert(Inpu tStream is)
    > {
    > try {
    > builderFactory = DocumentBuilder Factory.newInst ance();
    >
    > docFactory = builderFactory. newDocumentBuil der();
    >
    > inDoc = docFactory.pars e(is);
    > } catch(Exception e) {
    > e.printStackTra ce();
    > }
    > }
    >[/color]

    --
    Cordialement,

    ///
    (. .)
    -----ooO--(_)--Ooo-----
    | Philippe Poulard |
    -----------------------

    Comment

    Working...