Refine search
/** * Parse the given XML content to a {@link Document}. * @param xml the content to parse * @param encoding optional content encoding, if provided as metadata (e.g. in HTTP headers) * @return the parsed document */ protected Document parseXmlByteArray(byte[] xml, @Nullable String encoding) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(this.hasNamespaces); DocumentBuilder documentBuilder = factory.newDocumentBuilder(); InputSource inputSource = new InputSource(new ByteArrayInputStream(xml)); if (StringUtils.hasText(encoding)) { inputSource.setEncoding(encoding); } return documentBuilder.parse(inputSource); }
try { HttpEntity entity = response.getEntity(); final InputStream in = entity.getContent(); final SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); final XmlHandler handler = new XmlHandler(); Reader reader = new InputStreamReader(in, "UTF-8"); InputSource is = new InputSource(reader); is.setEncoding("UTF-8"); parser.parse(is, handler); //TODO: get the data from your handler } catch (final Exception e) { Log.e("ParseError", "Error parsing xml", e); }
private boolean doesSaxSupportCharacterSet(final String charSetName) throws ParserConfigurationException, SAXException, IOException { final byte[] data = ("<?xml version=\"1.0\" encoding=\"" + charSetName + "\"?><Z/>").getBytes(charSetName); final DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); try { final InputSource is = new InputSource(new ByteArrayInputStream(data)); is.setEncoding(charSetName); documentBuilder.parse(is); } catch (final SAXParseException e) { if (e.getMessage().contains(charSetName)) { return false; } } return true; } }
InputStream inputStream = encodedResource.getResource().getInputStream(); try { InputSource inputSource = new InputSource(inputStream); if (encodedResource.getEncoding() != null) { inputSource.setEncoding(encodedResource.getEncoding());
InputSource inputSource = new InputSource(streamSource.getInputStream()); inputSource.setEncoding(getDefaultEncoding()); return unmarshalSaxSource(new SAXSource(inputSource)); return unmarshalSaxSource(new SAXSource(new InputSource(streamSource.getReader())));
InputStream inputStream = encodedResource.getResource().getInputStream(); try { InputSource inputSource = new InputSource(inputStream); if (encodedResource.getEncoding() != null) { inputSource.setEncoding(encodedResource.getEncoding());
protected Document dom(InputStream stream, boolean skipDTD, String encoding) throws ParserConfigurationException, SAXException, IOException { InputSource input = new InputSource(stream); if (encoding != null) { input.setEncoding(encoding); } else { input.setEncoding(Charset.defaultCharset().name()); } if (skipDTD) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(false); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setEntityResolver(new EmptyResolver()); Document dom = builder.parse(input); return dom; } else { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse(input); } }
public InputSource asInputSource() { InputSource source = new InputSource(this); source.setEncoding(charset.name()); return source; }
public InputSource toInputSource() { final InputSource is = new InputSource(new ByteArrayInputStream(data)); is.setEncoding(charset.name()); return is; } }
/** * Reads a Document from the given <code>Reader</code> using SAX * * @param reader * is the reader for the input * * @return the newly created Document instance * * @throws DocumentException * if an error occurs during parsing. */ public Document read(Reader reader) throws DocumentException { InputSource source = new InputSource(reader); if (this.encoding != null) { source.setEncoding(this.encoding); } return read(source); }
/** * <p> * Reads a Document from the given stream using SAX * </p> * * @param in * <code>InputStream</code> to read from. * * @return the newly created Document instance * * @throws DocumentException * if an error occurs during parsing. */ public Document read(InputStream in) throws DocumentException { InputSource source = new InputSource(in); if (this.encoding != null) { source.setEncoding(this.encoding); } return read(source); }
/** * Parses an InputStream as DOM. * @param in the inputstream to parse. * @param encoding the encoding of the input stream, if not null. * @return the parsed XML DOM. */ public static Document fromInputStream(InputStream in, String encoding) { InputSource is = new InputSource(in); if (encoding != null) { is.setEncoding(encoding); } return fromInputSource(is); }
public XMLValidationSchema createSchema(InputStream in, String encoding, String publicId, String systemId) throws XMLStreamException { InputSource src = new InputSource(in); src.setEncoding(encoding); src.setPublicId(publicId); src.setSystemId(systemId); return loadSchema(src, systemId); }
/** * Parses an InputStream as DOM. * @param in the inputstream to parse. * @param encoding the encoding of the input stream, if not null. * @return the parsed XML DOM. */ public static Document fromInputStream(InputStream in, String encoding) { InputSource is = new InputSource(in); if (encoding != null) { is.setEncoding(encoding); } return fromInputSource(is); }
/** * Parses an InputStream as DOM. * @param in the inputstream to parse. * @param encoding the encoding of the input stream, if not null. * @return the parsed XML DOM. */ public static Document fromInputStream(InputStream in, String encoding) { InputSource is = new InputSource(in); if (encoding != null) { is.setEncoding(encoding); } return fromInputSource(is); }
/** * <p> * Reads a Document from the given <code>URL</code> using SAX * </p> * * @param url * <code>URL</code> to read from. * * @return the newly created Document instance * * @throws DocumentException * if an error occurs during parsing. */ public Document read(URL url) throws DocumentException { String systemID = url.toExternalForm(); InputSource source = new InputSource(systemID); if (this.encoding != null) { source.setEncoding(this.encoding); } return read(source); }
/** * <p> * Reads a Document from the given stream using SAX * </p> * * @param in * <code>InputStream</code> to read from. * @param systemId * is the URI for the input * * @return the newly created Document instance * * @throws DocumentException * if an error occurs during parsing. */ public Document read(InputStream in, String systemId) throws DocumentException { InputSource source = new InputSource(in); source.setSystemId(systemId); if (this.encoding != null) { source.setEncoding(this.encoding); } return read(source); }
/** * <p> * Reads a Document from the given <code>Reader</code> using SAX * </p> * * @param reader * is the reader for the input * @param systemId * is the URI for the input * * @return the newly created Document instance * * @throws DocumentException * if an error occurs during parsing. */ public Document read(Reader reader, String systemId) throws DocumentException { InputSource source = new InputSource(reader); source.setSystemId(systemId); if (this.encoding != null) { source.setEncoding(this.encoding); } return read(source); }
/** * <p> * Reads a Document from the given URL or filename using SAX. * </p> * * <p> * If the systemId contains a <code>':'</code> character then it is * assumed to be a URL otherwise its assumed to be a file name. If you want * finer grained control over this mechansim then please explicitly pass in * either a {@link URL}or a {@link File}instance instead of a {@link * String} to denote the source of the document. * </p> * * @param systemId * is a URL for a document or a file name. * * @return the newly created Document instance * * @throws DocumentException * if an error occurs during parsing. */ public Document read(String systemId) throws DocumentException { InputSource source = new InputSource(systemId); if (this.encoding != null) { source.setEncoding(this.encoding); } return read(source); }
@SuppressWarnings("unchecked") protected T processWithSecureProcessing(Unmarshaller unmarshaller, InputStream entityStream, String charset) throws JAXBException { unmarshaller = new SecureUnmarshaller(unmarshaller, disableExternalEntities, enableSecureProcessingFeature, disableDTDs); if (charset == null) { InputSource is = new InputSource(entityStream); is.setEncoding(StandardCharsets.UTF_8.name()); return (T) unmarshaller.unmarshal(is); } else { return (T) unmarshaller.unmarshal(entityStream); } } }