Example usage for javax.xml.parsers DocumentBuilderFactory setIgnoringElementContentWhitespace

List of usage examples for javax.xml.parsers DocumentBuilderFactory setIgnoringElementContentWhitespace

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilderFactory setIgnoringElementContentWhitespace.

Prototype


public void setIgnoringElementContentWhitespace(boolean whitespace) 

Source Link

Document

Specifies that the parsers created by this factory must eliminate whitespace in element content (sometimes known loosely as 'ignorable whitespace') when parsing XML documents (see XML Rec 2.10).

Usage

From source file:Main.java

private static DocumentBuilderFactory newDocumentBuilderFactory() {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setIgnoringComments(true);//from w ww  .  j  a va2s  . c om
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setExpandEntityReferences(false);
    return dbf;
}

From source file:Main.java

public static Map<String, String> readXmlToMap(String filename, String xpathExp, String arrtibute)
        throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
    //to-do something
    Map<String, String> dataMap = new HashMap<String, String>();

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setIgnoringElementContentWhitespace(true);

    DocumentBuilder db = dbf.newDocumentBuilder();

    File file = new File(filename);
    if (file.exists()) {
        Document doc = db.parse(file);
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();

        NodeList varList = (NodeList) xpath.evaluate(xpathExp, doc, XPathConstants.NODESET);

        for (int i = 0; i < varList.getLength(); i++) {
            dataMap.put(varList.item(i).getAttributes().getNamedItem(arrtibute).getNodeValue(),
                    varList.item(i).getTextContent());
        }/*  w ww .j  av a 2 s .  c  o m*/
    }
    return dataMap;
}

From source file:Main.java

public static Document fromXML(String xml) throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//from   w w w .  jav a2 s  .c o m
    factory.setIgnoringElementContentWhitespace(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
}

From source file:Main.java

public static Document fromXML(String xml) throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//ww w  .  jav  a 2s  .c o  m
    factory.setIgnoringElementContentWhitespace(true);
    DocumentBuilder builder = factory.newDocumentBuilder();

    return builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
}

From source file:Main.java

public static Document load(File fileName) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(false);// w  w w. j  a va 2s  .com
    factory.setIgnoringElementContentWhitespace(true);
    factory.setValidating(false);
    factory.setCoalescing(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(fileName);
}

From source file:Main.java

/**
 * Creates and configures a DocumentBuilderFactory for use with DOM. The
 * returned DocumentBuilderFactory is namespace aware and the parsers
 * constructed with it will not eliminate whitespace in elements.
 * /*from w  w  w  .j a  v  a 2 s.c  o  m*/
 * @return Properly configured DocumentBuilderFactory
 */
static DocumentBuilderFactory createDOMFactory() {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    domFactory.setIgnoringElementContentWhitespace(false);
    return domFactory;
}

From source file:Main.java

public static Document parseFile(String fileName) {
    System.out.println("Parsing XML file... " + fileName);

    DocumentBuilder docBuilder;//from  w ww. j ava  2  s.co  m
    Document doc = null;
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setIgnoringElementContentWhitespace(true);
    try {
        docBuilder = docBuilderFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        System.out.println("Wrong parser configuration: " + e.getMessage());
        return null;
    }
    File sourceFile = new File(fileName);
    try {
        doc = docBuilder.parse(sourceFile);
    } catch (SAXException e) {
        System.out.println("Wrong XML file structure: " + e.getMessage());
        return null;
    } catch (IOException e) {
        System.out.println("Could not read source file: " + e.getMessage());
    }

    return doc;
}

From source file:Main.java

/**
 * Create a map based on a xml file and a xpath expression and an attribute.
 * Treat attribute's value as map's key, treat xpath expression represented node's text content as map's value.
 * //w w  w . j a  v  a 2  s. c o m
 * @param xmlFile handled xml file
 * @param xpathExp xpath express, such as "//var"
 * @param arrtibute attribute, such as "name"
 * @return created Map object
 * @throws Exception
 */
public static Map<String, String> readXmlToMap(String xmlFile, String xpathExp, String arrtibute)
        throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringElementContentWhitespace(true);

    DocumentBuilder db = factory.newDocumentBuilder();
    Document xmlDoc = null;
    try {
        xmlDoc = db.parse(new File(xmlFile));
    } catch (SAXException e) {
        if ("Premature end of file.".equals(e.getMessage()))
            System.out.println("Maybe your local data file has no content.");
        //e.printStackTrace();
        return new HashMap<String, String>();
    }
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    NodeList varList = (NodeList) xpath.evaluate(xpathExp, xmlDoc, XPathConstants.NODESET);
    Map<String, String> dataMap = new HashMap<String, String>();
    for (int i = 0; i < varList.getLength(); i++)
        dataMap.put(varList.item(i).getAttributes().getNamedItem(arrtibute).getNodeValue(),
                varList.item(i).getTextContent());

    return dataMap;
}

From source file:Main.java

/**
 * Parses contents of given XML file and returns DOM Object
 * @param fileName Path of XML file /*from w w  w.  j a  v  a2  s .c om*/
 * @return
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static Document parseXMLFile(final File xmlFile)
        throws ParserConfigurationException, SAXException, IOException {

    if (xmlFile == null)
        return null;

    Document doc = null;
    DocumentBuilder docBuilder;
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setIgnoringElementContentWhitespace(true);
    docBuilder = docBuilderFactory.newDocumentBuilder();

    doc = docBuilder.parse(xmlFile);
    return doc;
}

From source file:Main.java

public static Document loadString(String paramString) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(false);//from   w  w  w . j a  v a  2s . c  om
    factory.setIgnoringElementContentWhitespace(false);
    factory.setValidating(false);
    factory.setCoalescing(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    char[] arrayOfChar = new char[paramString.length()];
    paramString.getChars(0, paramString.length(), arrayOfChar, 0);
    InputSource input = new InputSource(new CharArrayReader(arrayOfChar));
    return builder.parse(input);
}