public ArrayList<String> parseXML() throws Exception { ArrayList<String> ret = new ArrayList<String>(); handshake(); URL url = new URL( "http://mangaonweb.com/page.do?cdn=" + cdn + "&cpn=book.xml&crcod=" + crcod + "&rid=" + (int) (Math.random() * 10000)); String page = DownloaderUtils.getPage(url.toString(), "UTF-8", cookies); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(page)); Document d = builder.parse(is); Element doc = d.getDocumentElement(); NodeList pages = doc.getElementsByTagName("page"); total = pages.getLength(); for (int i = 0; i < pages.getLength(); i++) { Element e = (Element) pages.item(i); ret.add(e.getAttribute("path")); } return (ret); }
/** * Create from factory a DocumentBuilder and let it create a org.w3c.dom.Document. This method * takes InputSource. After successful finish the document tree is returned. * * @param input a parser input (for URL users use: <code>new InputSource(url.toExternalForm()) * </code> * @param validate if true validating parser is used * @param namespaceAware if true DOM is created by namespace aware parser * @param errorHandler a error handler to notify about exception or <code>null</code> * @param entityResolver SAX entity resolver or <code>null</code>; see class Javadoc for hints * @throws IOException if an I/O problem during parsing occurs * @throws SAXException is thrown if a parser error occurs * @throws FactoryConfigurationError Application developers should never need to directly catch * errors of this type. * @return document representing given input, or null if a parsing error occurs */ public static Document parse( InputSource input, boolean validate, boolean namespaceAware, ErrorHandler errorHandler, EntityResolver entityResolver) throws IOException, SAXException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(validate); factory.setNamespaceAware(namespaceAware); DocumentBuilder builder = null; try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException ex) { throw new SAXException( "Cannot create parser satisfying configuration parameters", ex); // NOI18N } if (errorHandler != null) { builder.setErrorHandler(errorHandler); } if (entityResolver != null) { builder.setEntityResolver(entityResolver); } return builder.parse(input); }
private void guardaRes(String resu, CliGol cliGol) throws ParserConfigurationException, SAXException, IOException { String[] nodos = {"NoHit", "Hit", "Buro"}; DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputSource is = new InputSource(new StringReader(resu)); Document xml = db.parse(is); Element raiz = xml.getDocumentElement(); String nombre = obtenerNombre(raiz); for (String s : nodos) { Node nodo = raiz.getElementsByTagName(s) == null ? null : raiz.getElementsByTagName(s).item(0); if (nodo != null) { String informacion = sustraerInformacionNodo(cliGol, nodo); guardarEnListas(nodo.getNodeName(), nombre + "," + informacion); } } }
/** * Creates an AppConfig using class <code>pAppClass</code> to search for the config file resource. * That class must have a resource file <code>AppConfig.xml</code>. */ private AppConfig(InputSource pInputSource) throws InvalidInputException, DataNotFoundException { try { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = builder.parse(pInputSource); mConfigFileDocument = document; } catch (Exception e) { e.printStackTrace(System.err); throw new InvalidInputException("unable to load XML configuration file", e); } }
/** * Advanced users only; use loadXML() in PApplet. * * <p>Added extra code to handle \u2028 (Unicode NLF), which is sometimes inserted by web browsers * (Safari?) and not distinguishable from a "real" LF (or CRLF) in some text editors (i.e. * TextEdit on OS X). Only doing this for XML (and not all Reader objects) because LFs are * essential. https://github.com/processing/processing/issues/2100 * * @nowebref */ public XML(final Reader reader, String options) throws IOException, ParserConfigurationException, SAXException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // Prevent 503 errors from www.w3.org try { factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); } catch (IllegalArgumentException e) { // ignore this; Android doesn't like it } // without a validating DTD, this doesn't do anything since it doesn't know what is ignorable // factory.setIgnoringElementContentWhitespace(true); factory.setExpandEntityReferences(false); // factory.setExpandEntityReferences(true); // factory.setCoalescing(true); // builderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); DocumentBuilder builder = factory.newDocumentBuilder(); // builder.setEntityResolver() // SAXParserFactory spf = SAXParserFactory.newInstance(); // spf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); // SAXParser p = spf.newSAXParser(); // builder = DocumentBuilderFactory.newDocumentBuilder(); // builder = new SAXBuilder(); // builder.setValidation(validating); Document document = builder.parse( new InputSource( new Reader() { @Override public int read(char[] cbuf, int off, int len) throws IOException { int count = reader.read(cbuf, off, len); for (int i = 0; i < count; i++) { if (cbuf[off + i] == '\u2028') { cbuf[off + i] = '\n'; } } return count; } @Override public void close() throws IOException { reader.close(); } })); node = document.getDocumentElement(); }
/** @param name creates a node with this name */ public XML(String name) { try { // TODO is there a more efficient way of doing this? wow. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.newDocument(); node = document.createElement(name); this.parent = null; } catch (ParserConfigurationException pce) { throw new RuntimeException(pce); } }
public static void main(String args[]) { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(args[0]); } catch (SAXException e) { // e.printStackTrace(); System.out.println(e.getMessage()); } catch (Exception e) { // e.printStackTrace(); } }
static { try { URL url = SpellCheckActivator.bundleContext.getBundle().getResource(RESOURCE_LOC); InputStream stream = url.openStream(); if (stream == null) throw new IOException(); // strict parsing options DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setIgnoringComments(true); factory.setIgnoringElementContentWhitespace(true); // parses configuration xml /*- * Warning: Felix is unable to import the com.sun.rowset.internal * package, meaning this can't use the XmlErrorHandler. This causes * a warning and a default handler to be attached. Otherwise this * should have: builder.setErrorHandler(new XmlErrorHandler()); */ DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(stream); // iterates over nodes, parsing contents Node root = doc.getChildNodes().item(1); NodeList categories = root.getChildNodes(); for (int i = 0; i < categories.getLength(); ++i) { Node node = categories.item(i); if (node.getNodeName().equals(NODE_DEFAULTS)) { parseDefaults(node.getChildNodes()); } else if (node.getNodeName().equals(NODE_LOCALES)) { parseLocales(node.getChildNodes()); } else { logger.warn("Unrecognized category: " + node.getNodeName()); } } } catch (IOException exc) { logger.error("Unable to load spell checker parameters", exc); } catch (SAXException exc) { logger.error("Unable to parse spell checker parameters", exc); } catch (ParserConfigurationException exc) { logger.error("Unable to parse spell checker parameters", exc); } }
/** * Writes a table to a XML-file * * @param t - Output Model * @param destination - File Destination */ public static void writeXML(Model t, String destination) { try { // Create the XML document builder, and document that will be used DocumentBuilderFactory xmlBuilder = DocumentBuilderFactory.newInstance(); DocumentBuilder Builder = xmlBuilder.newDocumentBuilder(); Document xmldoc = Builder.newDocument(); // create Document node, and get it into the file Element Documentnode = xmldoc.createElement("SPREADSHEET"); xmldoc.appendChild(Documentnode); // create element nodes, and their attributes (Cells, and row/column // data) and their content for (int row = 1; row < t.getRows(); row++) { for (int col = 1; col < t.getCols(col); col++) { Element cell = xmldoc.createElement("CELL"); // set attributes cell.setAttribute("column", Integer.toString(col)); cell.setAttribute("row", Integer.toString(col)); // set content cell.appendChild(xmldoc.createTextNode((String) t.getContent(row, col))); // append node to document node Documentnode.appendChild(cell); } } // Creating a datastream for the DOM tree TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); // Indentation to make the XML file look better transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // remove the java version transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); DOMSource stream = new DOMSource(xmldoc); StreamResult target = new StreamResult(new File(destination)); // write the file transformer.transform(stream, target); } catch (ParserConfigurationException e) { System.out.println("Can't create the XML document builder"); } catch (TransformerConfigurationException e) { System.out.println("Can't create transformer"); } catch (TransformerException e) { System.out.println("Can't write to file"); } }
private void createDOM() throws ParserConfigurationException { if (dbf == null) { dbf = DocumentBuilderFactory.newInstance(); db = dbf.newDocumentBuilder(); dil = (DOMImplementationLS) db.getDOMImplementation(); dw = dil.createDOMWriter(); } }
/** * Method that reads a XML-file, and returns a Model that contains the information * * @param file * @return * @return */ public static Model readXML(String file) { // initialize table to be filled with content of XML file Model t = new Model(); try { // Create file to be parsed by document parser File xmlfile = new File(file); // create parser DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); // parse the file Document parsedfile = parser.parse(xmlfile); // normalize the parsed file (make it more user-friendly) parsedfile.getDocumentElement().normalize(); NodeList cells = parsedfile.getElementsByTagName("CELL"); for (int i = 0; i < cells.getLength(); i++) { // Get cell at list index i Node currentcell = cells.item(i); // read the elements "location" attributes row/column if (Node.ELEMENT_NODE == currentcell.getNodeType()) { Element cellinfo = (Element) currentcell; // get the row number from node attribute int row = Integer.parseInt(cellinfo.getAttribute("row")) - 1; // get the column number from the node attribute int col = Integer.parseInt(cellinfo.getAttribute("column")) - 1; // get content from node String content = cellinfo.getTextContent(); if (content != null) { content = content.replace("\n", ""); } // Make the content an Integer (if it is a number), easier // for // using it later on // put content in table, with row/column inserted as x/y t.setContent(row, col, (String) content); } } } catch (ParserConfigurationException e) { System.out.println("Fileparser could not be made"); } catch (IOException f) { System.out.println("File could not be parsed, did you enter the correct file name?"); } catch (SAXException g) { System.out.println("Something went wrong in parsing the file"); } return t; }
/** * Unlike the loadXML() method in PApplet, this version works with files that are not in UTF-8 * format. * * @nowebref */ public XML(InputStream input, String options) throws IOException, ParserConfigurationException, SAXException { // this(PApplet.createReader(input), options); // won't handle non-UTF8 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { // Prevent 503 errors from www.w3.org factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); } catch (IllegalArgumentException e) { // ignore this; Android doesn't like it } factory.setExpandEntityReferences(false); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new InputSource(input)); node = document.getDocumentElement(); }
/** * Reads an XML document from an input source and copies its values into the specified object * * @param ob The object to receive the values * @param source The location of the XML document * @throws IOException If there is an error reading the document */ public void readObject(Object ob, InputSource source) throws IOException { try { // Create a document builder to read the document DocumentBuilder builder = factory.newDocumentBuilder(); // Read the document Document doc = builder.parse(source); // Get the root element Element element = doc.getDocumentElement(); // Copy the root element into the bean readObject(ob, element); } catch (SAXException exc) { throw new IOException("Error parsing XML document: " + exc.toString()); } catch (ParserConfigurationException exc) { throw new IOException("Error parsing XML document: " + exc.toString()); } }
private Document GetXMLDocument(String url) { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputStream input = HTMLTools.inputStream_GET(url, 5000); InputStreamReader reader = new InputStreamReader(input, ENCODING_UTF8); InputSource inSrc = new InputSource(reader); inSrc.setEncoding(ENCODING_UTF8); return db.parse(inSrc); } catch (ParserConfigurationException pce) { Print.logError("Parse error: " + pce); return null; } catch (SAXException se) { Print.logError("Parse error: " + se); return null; } catch (IOException ioe) { Print.logError("IO error: " + ioe); return null; } }
private void create() { try { tracks = new Vector(); hash = new Hashtable(); createDOM(); doc = db.newDocument(); docElt = doc.createElement(docElementName); doc.appendChild(docElt); } catch (Exception e) { e.printStackTrace(); } }
public void load(InputStream is) throws IOException, ParserConfigurationException, SAXException { doc = db.parse(is); docElt = doc.getDocumentElement(); if (docElt.getTagName().equals(docElementName)) { NodeList nl = docElt.getElementsByTagName(trackElementName); for (int i = 0; i < nl.getLength(); i++) { Element elt = (Element) nl.item(i); Track track = new Track(elt); tracks.add(track); hash.put(track.getKey(), track); } } }
/** Forward to real factory, set error handler */ public DocumentBuilder newDocumentBuilder() throws ParserConfigurationException { DocumentBuilder db = fact.newDocumentBuilder(); log.debug3("Created builder: " + db); db.setErrorHandler(new MyErrorHandler()); return db; }
protected Document parse(InputStream inputStream) throws LibraryException { Map<Integer, SongBean> songMap = new HashMap<Integer, SongBean>(); Map<Integer, AlbumBean> albumMap = new HashMap<Integer, AlbumBean>(); Map<Integer, AlbumBean> secondaryAlbumMap = new HashMap<Integer, AlbumBean>(); warningList.clear(); try { // Create a builder factory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setNamespaceAware(true); factory.setIgnoringElementContentWhitespace(true); factory.setIgnoringComments(true); // Create the builder and parse the file DocumentBuilder builder = factory.newDocumentBuilder(); // Set an error listener and parse the document builder.setErrorHandler(new iTradeTunesLibraryErrorHandler()); builder.setEntityResolver(new iTradeTunesLibraryResolver()); Document document = builder.parse(inputStream); synchronized (libraryList) { // Query the library document and build the library list XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression xPathExpression = xPath.compile(XPATH_LIBRARY_LIST); NodeList nodelist = (NodeList) xPathExpression.evaluate(document, XPathConstants.NODESET); // Process the elements in the nodelist SongBean song = null; for (int i = 0; i < nodelist.getLength(); i++) { boolean isTrackID = false; // Get element and child nodes Element elem = (Element) nodelist.item(i); NodeList list = elem.getChildNodes(); // Get node value Node childKey = list.item(0); String key = childKey.getNodeValue(); // Check if we have to create a new bean if (SongBean.NAME_TRACK_ID.equals(key)) { isTrackID = true; SongBean previousSong = song; song = new SongBean(); if (previousSong != null && !("AAC audio file".equals(previousSong.getKind()) || "MPEG audio file".equals(previousSong.getKind()))) { songMap.remove(previousSong.getTrack_ID()); } else { // Add an album bean addOrUpdateAlbum(albumMap, previousSong, false); } } // The first parameter is the key String prop = childKey.getNodeValue().replace(' ', '_'); // The second parameter is the value i++; // Get element and child nodes elem = (Element) nodelist.item(i); // Check for boolean properties Object value = null; // Get node value list = elem.getChildNodes(); childKey = list.item(0); value = (childKey == null) ? elem.getNodeName() : childKey.getNodeValue(); if (isTrackID) { isTrackID = false; } // Set the property of the song bean Statement stmt = new Statement(song, "set" + prop, new Object[] {value}); try { stmt.execute(); } catch (Exception e) { // Ignore that field, we do not have it in our bean } // If the property is the track ID, add the song to the hash // map if (SongBean.NAME_TRACK_ID.equals(key)) { int trackID = Integer.valueOf((String) value); songMap.put(trackID, song); } } // Update album for last song addOrUpdateAlbum(albumMap, song, false); // Check the album map for inconsistencies Iterator<AlbumBean> albums = albumMap.values().iterator(); while (albums.hasNext()) { AlbumBean album = albums.next(); if (album.checkConsistency()) { libraryList.add(album); album.setHashCode(); } else { // Add an inconsistent album only using the album title SongBean[] songs = album.getSongs(); for (int i = 0; i < songs.length; i++) { addOrUpdateAlbum(secondaryAlbumMap, songs[i], true); } } } // Check secondary album map for consistency albums = secondaryAlbumMap.values().iterator(); while (albums.hasNext()) { AlbumBean album = albums.next(); if (album.checkConsistency()) { libraryList.add(album); album.setHashCode(); } else { // This album cannot be matched // TODO: Add to warning message } } setChanged(); } return document; } catch (IOException ioe) { // Log an expected connect exception throw new LibraryException(ioe); } catch (SAXException se) { // Catch all other exceptions throw new LibraryException(se); } catch (ParserConfigurationException pce) { // Catch all other exceptions Utils.logSevere(pce); throw new LibraryException(pce); } catch (XPathExpressionException xpe) { // Catch all other exceptions Utils.logSevere(xpe); throw new LibraryException(xpe); } catch (NumberFormatException nfe) { // Catch all other exceptions throw new LibraryException(nfe); } }
public static MiningResult importFile(InputStream input) throws IOException { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); Document doc; // NodeList netNodes; dbf.setValidating(false); dbf.setIgnoringComments(true); dbf.setIgnoringElementContentWhitespace(true); // dbf.setExpandEntityReferences(false); // dbf.setNamespaceAware(false); DocumentBuilder db = dbf.newDocumentBuilder(); db.setEntityResolver( new EntityResolver() { public InputSource resolveEntity(String publicId, String systemId) { if (systemId.indexOf("ARIS-Export") != -1) { return new InputSource("file:" + About.EXTLIBLOCATION() + "ARIS-Export101.dtd"); } else { return null; } } }); InputSource inpStream = new InputSource(input); inpStream.setSystemId("file:" + System.getProperty("user.dir", "")); doc = db.parse(inpStream); // check if root element is a aml tag Message.add("parsing done" + doc, Message.DEBUG); if (!(doc.getDocumentElement().getNodeName().equals("AML"))) { Message.add("aml tag not found", Message.ERROR); throw new Exception("aml tag not found"); } else { Message.add("aml root element found"); } EPCResult result = new EPCResult(null, (EPC) null); HashMap ObjDef_LinkId = new HashMap(); HashMap modelid_net = new HashMap(); HashMap ObjDef_Name = new HashMap(); HashMap function_LinkId = new HashMap(); HashMap ModelId_ModelType = new HashMap(); traverseAMLforObjectNames( ObjDef_Name, doc.getDocumentElement(), ObjDef_LinkId, ModelId_ModelType); Iterator findLinkToEpc = ObjDef_LinkId.keySet().iterator(); while (findLinkToEpc.hasNext()) { String currentObjDef = (String) findLinkToEpc.next(); String Links = (String) ObjDef_LinkId.get(currentObjDef); StringTokenizer linkSet = new StringTokenizer(Links); String realEpcLink = ""; while (linkSet.hasMoreTokens()) { String currentLink = linkSet.nextToken(); if (ModelId_ModelType.get(currentLink).equals("MT_EEPC")) { realEpcLink = currentLink; break; } } if (realEpcLink.equals(" ")) { ObjDef_LinkId.remove(currentObjDef); } else { ObjDef_LinkId.put(currentObjDef, realEpcLink); } } result = traverseAML( result, doc.getDocumentElement(), null, ObjDef_Name, ObjDef_LinkId, modelid_net, function_LinkId); Iterator hierarchicalFunctions = function_LinkId.keySet().iterator(); while (hierarchicalFunctions.hasNext()) { EPCSubstFunction f = (EPCSubstFunction) hierarchicalFunctions.next(); f.setSubstitutedEPC((EPC) modelid_net.get(function_LinkId.get(f))); // Message.add(f.getSubstitutedEPC().getName()); } return result; } catch (Throwable x) { Message.add(x.toString()); throw new IOException(x.getMessage()); } }
@Override protected DocumentBuilder getDocBuilder() throws ParserConfigurationException { DocumentBuilder builder = super.getDocBuilder(); builder.setErrorHandler(getErrorHandler()); return builder; }