Java XML Parse File parseFile(Class context, String name)

Here you can find the source of parseFile(Class context, String name)

Description

Parse an XHTML file from our CLASSPATH and return the instance.

License

Apache License

Declaration

public static Document parseFile(Class<?> context, String name) throws IOException 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import org.w3c.dom.Document;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.InputStream;

import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

public class Main {
    /** Parse an XHTML file from our CLASSPATH and return the instance. */
    public static Document parseFile(Class<?> context, String name) throws IOException {
        try (InputStream in = context.getResourceAsStream(name)) {
            if (in == null) {
                return null;
            }/*from   w  w w .  j a va 2s.c  om*/
            Document doc = newBuilder().parse(in);
            compact(doc);
            return doc;
        } catch (SAXException | ParserConfigurationException | IOException e) {
            throw new IOException("Error reading " + name, e);
        }
    }

    /** Parse an XHTML file from the local drive and return the instance. */
    public static Document parseFile(Path path) throws IOException {
        try (InputStream in = Files.newInputStream(path)) {
            Document doc = newBuilder().parse(in);
            compact(doc);
            return doc;
        } catch (NoSuchFileException e) {
            return null;
        } catch (SAXException | ParserConfigurationException | IOException e) {
            throw new IOException("Error reading " + path, e);
        }
    }

    private static DocumentBuilder newBuilder() throws ParserConfigurationException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);
        factory.setExpandEntityReferences(false);
        factory.setIgnoringComments(true);
        factory.setCoalescing(true);
        return factory.newDocumentBuilder();
    }

    private static void compact(Document doc) {
        try {
            String expr = "//text()[normalize-space(.) = '']";
            XPathFactory xp = XPathFactory.newInstance();
            XPathExpression e = xp.newXPath().compile(expr);
            NodeList empty = (NodeList) e.evaluate(doc, XPathConstants.NODESET);
            for (int i = 0; i < empty.getLength(); i++) {
                Node node = empty.item(i);
                node.getParentNode().removeChild(node);
            }
        } catch (XPathExpressionException e) {
            // Don't do the whitespace removal.
        }
    }
}

Related

  1. parse(String filename)
  2. parse(String xmlFile)
  3. parseAIMLFile(File file)
  4. parseData(String filename)
  5. parseDoc(File xmlFile)
  6. parseFile(File file)
  7. parseFile(File fn)
  8. parseFile(final File aFile)
  9. parseFile(String fileName)