List of usage examples for javax.xml.parsers ParserConfigurationException getMessage
public String getMessage()
From source file:Main.java
/** * Parse the content of the given file as an XML document and return a new DOM * /*from w w w .ja v a 2 s. c om*/ * @param fileName * @return xml Document * @throws Exception */ public static Document readXmlFile(String fileName) throws Exception { Document doc = null; try { File xmlFile = new File(fileName); DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(false); DocumentBuilder builder = domFactory.newDocumentBuilder(); doc = builder.parse(xmlFile); } catch (ParserConfigurationException e) { throw new Exception(e.getMessage(), e); } catch (Exception e) { throw new Exception(e.getMessage(), e); } return doc; }
From source file:Main.java
private static org.w3c.dom.Document getXMLDocument(String url) { Document document = null;//from w w w . j a v a2 s. c o m DocumentBuilderFactory builderFactory; DocumentBuilder parser; // Try to load the document at the specified filepath into a DOM structure. // try { builderFactory = DocumentBuilderFactory.newInstance(); parser = builderFactory.newDocumentBuilder(); document = parser.parse(url); } catch (ParserConfigurationException p) { System.out.println("Error creating DOM parser."); System.out.println(" " + p.getMessage()); } catch (SAXException s) { System.out.println("XML document returned is not well-formed."); System.out.println(" URL : " + url); System.out.println(" " + s.getMessage()); } catch (IOException i) { System.out.println("Error accessing the stream."); System.out.println(" URL : " + url); System.out.println(" " + i.getMessage()); document = null; } return document; }
From source file:gov.hhs.fha.nhinc.lift.proxy.util.ProxyUtil.java
public static org.w3c.dom.Element marshal(Object obj) throws JAXBException { try {/* w w w .j a va 2s. c o m*/ Document doc = null; JAXBContext jc = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = jc.createMarshaller(); javax.xml.parsers.DocumentBuilderFactory dbf; dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); doc = dbf.newDocumentBuilder().newDocument(); marshaller.marshal(obj, doc); log.info("Marshal: " + doc.getNodeValue()); return doc.getDocumentElement(); } catch (ParserConfigurationException ex) { throw new JAXBException("Unable to create document: " + ex.getMessage()); } }
From source file:Main.java
/** * This will parse an XML stream and create a DOM document. * /*from ww w. j a v a 2s.c om*/ * @param fileName * The file to get the XML from. * @return The DOM document. * @throws ParserConfigurationException * @throws IOException * It there is an error creating the dom. * @throws ParserConfigurationException * @throws IOException * @throws SAXException * @throws SAXException * @throws ParseException */ public static Document parse(String fileName) throws IOException, ParseException { final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = null; try { builder = builderFactory.newDocumentBuilder(); } catch (final ParserConfigurationException e) { final ParseException thrown = new ParseException(e.getMessage(), 0); throw thrown; } try { return builder.parse(fileName); } catch (final IOException e) { final IOException thrown = new IOException(e.getMessage()); throw thrown; } catch (final SAXException e) { final ParseException thrown = new ParseException(e.getMessage(), 0); throw thrown; } }
From source file:Main.java
/** * This will parse an InputSource and create a DOM document. * //w w w . java 2s .c o m * @param is * The stream to get the XML from. * @return The DOM document. * @throws IOException * It there is an error creating the dom. * @throws ParseException */ public static Document parse(InputSource is) throws IOException, ParseException { final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = null; try { builder = builderFactory.newDocumentBuilder(); } catch (final ParserConfigurationException e) { final ParseException thrown = new ParseException(e.getMessage(), 0); throw thrown; } try { return builder.parse(is); } catch (final IOException e) { final IOException thrown = new IOException(e.getMessage()); throw thrown; } catch (final SAXException e) { final ParseException thrown = new ParseException(e.getMessage(), 0); throw thrown; } }
From source file:Main.java
/** * This will parse an XML stream and create a DOM document. * // w w w . j av a 2 s . c o m * @param is * The stream to get the XML from. * @return The DOM document. * @throws IOException * @throws IOException * It there is an error creating the dom. * @throws ParseException */ public static Document parse(InputStream is) throws IOException, ParseException { final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = null; try { builder = builderFactory.newDocumentBuilder(); } catch (final ParserConfigurationException e) { final ParseException thrown = new ParseException(e.getMessage(), 0); throw thrown; } try { return builder.parse(is); } catch (final IOException e) { final IOException thrown = new IOException(e.getMessage()); throw thrown; } catch (final SAXException e) { final ParseException thrown = new ParseException(e.getMessage(), 0); throw thrown; } }
From source file:com.casewaresa.framework.util.LegacyJasperInputStream.java
public static Document convertInputStreamToDOM(InputStream is) { Document document = null;// w w w .j ava 2 s . c o m InputSource isou = null; // BufferedInputStream bis = new BufferedInputStream(is); DocumentBuilder builder = null; try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Reader reader = new InputStreamReader(is); isou = new InputSource(reader); // isou.setEncoding("ISO-8859-1"); builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException ex) { log.error(ex.getMessage(), ex); } // catch (UnsupportedEncodingException e) { // log.error(e); // } try { document = builder.parse(isou); } catch (SAXException ex) { log.error(ex.getMessage(), ex); } catch (IOException ex) { log.error(ex.getMessage(), ex); } return document; }
From source file:ProcessorDemo.java
private static void readConfig(String confFile) { String parent = new File(confFile).getParentFile().getParent(); try {/*from w w w. j a v a 2 s .c om*/ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new InputSource(confFile)); NodeList nl = doc.getFirstChild().getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { org.w3c.dom.Node n = nl.item(i); if (n.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) { String nn = n.getNodeName(); String value = n.getFirstChild().getNodeValue(); if (nn.equals("composit")) { compositRule += value + "\n"; } if (nn.equals("compound")) { if (value.equals("\u69cb\u6210\u8a9e")) { isCompound = false; } } if (nn.equals("remark")) { remarkRule += value + "\n"; } if (nn.equals("dictionary")) { // read nested tag in <dictinary> NodeList dnl = n.getChildNodes(); for (int j = 0; j < dnl.getLength(); j++) { org.w3c.dom.Node dn = dnl.item(j); if (dn.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) { String dnn = dn.getNodeName(); if (dn.getFirstChild() == null) { throw new IllegalArgumentException("element '" + dnn + "' is empty"); } String dvalue = dn.getFirstChild().getNodeValue(); if (dnn.equals("compound")) { compoundFile = SenUtils.getPath(dvalue, parent); } } } } } } if (!isCompound) { try { ObjectInputStream is = new ObjectInputStream(new FileInputStream(compoundFile)); HashMap hashmap = (HashMap) is.readObject(); } catch (ClassNotFoundException e1) { throw new RuntimeException(e1); } } } catch (ParserConfigurationException e) { throw new IllegalArgumentException(e.getMessage()); } catch (FileNotFoundException e) { throw new IllegalArgumentException(e.getMessage()); } catch (SAXException e) { throw new IllegalArgumentException(e.getMessage()); } catch (IOException e) { throw new IllegalArgumentException(e.getMessage()); } }
From source file:net.ftb.util.AppUtils.java
/** * Reads XML from a stream/*ww w. j ava2s . com*/ * @param stream the stream to read the document from * @return The document * @throws IOException, SAXException if an error occurs when reading from the stream */ public static Document getXML(InputStream stream) throws IOException, SAXException { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); try { return docFactory.newDocumentBuilder().parse(stream); } catch (ParserConfigurationException ignored) { Logger.logError(ignored.getMessage(), ignored); } catch (UnknownHostException e) { Logger.logError(e.getMessage(), e); } return null; }
From source file:eu.scidipes.toolkits.palibrary.utils.XMLUtils.java
/** * Transforms a collection of {@link FormField} objects into a simple XML document * /*from w ww. j a v a2s .c o m*/ * @param fields * a collection of form fields * @return an xml document representing the form fields, or null if the passed collection is null or empty, or all * form fields have an empty value */ public static Node formFieldsToMetadata(final Collection<? extends FormField> fields) { if (fields != null && !fields.isEmpty()) { try { final Document xml; synchronized (documentBuilderFactory) { xml = documentBuilderFactory.newDocumentBuilder().newDocument(); } xml.setXmlStandalone(true); final Element metaData = xml.createElement("metadata"); int completedFieldCount = 0; for (final FormField field : fields) { if (!StringUtils.isEmpty(field.getValue())) { completedFieldCount++; final Element metaDataEntry = xml.createElement("metadataentry"); final Element entryName = xml.createElement("entryname"); final Element entryValue = xml.createElement("entryvalue"); entryName.setTextContent(field.getDisplayName()); entryValue.setTextContent(field.getValue()); metaDataEntry.appendChild(entryName); metaDataEntry.appendChild(entryValue); metaData.appendChild(metaDataEntry); } } if (completedFieldCount > 0) { xml.appendChild(metaData); return xml; } else { return null; } } catch (final ParserConfigurationException e) { LOG.error(e.getMessage(), e); } } return null; }