List of usage examples for org.jdom2.input SAXBuilder SAXBuilder
public SAXBuilder()
From source file:cz.muni.fi.pb138.scxml2voicexmlj.XmlHelper.java
public Document parseStream(InputStream xml) { try {//from w w w. j a v a 2 s . c o m SAXBuilder builder = new SAXBuilder(); return builder.build(xml); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:cz.natur.cuni.mirai.math.algebra.MiraiParser.java
License:Open Source License
public static void load(MathModel model, String filename) throws Exception { System.out.println("MiraiParser.load(" + filename + ")"); Element root = new SAXBuilder().build(new FileInputStream(filename), "UTF-8").getRootElement(); // keyboard input, characters and operators for (Object element : root.getChildren()) { String name = ((Element) element).getName(); if (name.equals(FORMULA)) { String text = ((Element) element).getText(); model.addElement(parse(model, text)); } else if (name.equals(LABEL)) { String label = ((Element) element).getText(); model.addElement(label);/*from w ww. j a v a 2 s . c om*/ } } model.setModified(false); // validate model model.firstFormula(); }
From source file:cz.natur.cuni.mirai.math.meta.MetaModel.java
License:Open Source License
public MetaModel(String filename) { try {/*www . j a va 2s . c o m*/ Element root = new SAXBuilder().build(MetaModel.class.getResourceAsStream(filename)).getRootElement(); // keyboard input, characters and operators parseComponents(root); } catch (Exception e) { System.out.println("parse failed: " + filename + "\n"); e.printStackTrace(); } }
From source file:cz.pecina.retro.memory.Snapshot.java
License:Open Source License
/** * Reads snapshot from a file and sets hardware accordingly. * * @param file input file//w w w .j a va 2 s . com */ public void read(final File file) { log.fine("Reading snapshot from a file, file: " + file.getName()); try { SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema(new StreamSource( getClass().getResourceAsStream("snapshot-" + SNAPSHOT_XML_FILE_VERSION + ".xsd"))) .newValidator().validate(new StreamSource(file)); } catch (final Exception exception) { log.fine("Error, validation failed, exception: " + exception.getMessage()); throw Application.createError(this, "validation"); } Document doc; Element snapshot; try { doc = new SAXBuilder().build(file); } catch (final JDOMException exception) { log.fine("Error, parsing failed, exception: " + exception.getMessage()); throw Application.createError(this, "parsing"); } catch (final Exception exception) { log.fine("Error, reading failed, exception: " + exception.getMessage()); throw Application.createError(this, "XMLRead"); } try { snapshot = doc.getRootElement(); } catch (final Exception exception) { log.fine("Error, parsing failed, exception: " + exception.getMessage()); throw Application.createError(this, "parsing"); } if (!snapshot.getName().equals("snapshot")) { log.fine("Error, parsing failed, no <snapshot> tag"); throw Application.createError(this, "parsing"); } if (!SNAPSHOT_XML_FILE_VERSION.equals(snapshot.getAttributeValue("version"))) { log.fine("Version mismatch"); throw Application.createError(this, "version"); } hardware.unmarshal(snapshot); log.fine("Reading completed"); }
From source file:cz.pecina.retro.memory.XML.java
License:Open Source License
/** * Reads XML data from a file and stores it in memory. * * @param file input file * @param destinationAddress destination address ({@code -1} = none) * @return info record/*w w w .j a v a 2s . c o m*/ */ public Info read(final File file, final int destinationAddress) { log.fine(String.format("Reading XML data from a file, file: %s, destination address: %04x", file.getName(), destinationAddress)); try { SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema(new StreamSource( getClass().getResourceAsStream("memory-" + MEMORY_XML_FILE_VERSION + ".xsd"))) .newValidator().validate(new StreamSource(file)); } catch (final Exception exception) { log.fine("Error, validation failed, exception: " + exception.getMessage()); throw Application.createError(this, "validation"); } Document doc; try { doc = new SAXBuilder().build(file); } catch (final JDOMException exception) { log.fine("Error, parsing failed, exception: " + exception.getMessage()); throw Application.createError(this, "parsing"); } catch (final Exception exception) { log.fine("Error, reading failed, exception: " + exception.getMessage()); throw Application.createError(this, "XMLRead"); } Element tag; try { tag = doc.getRootElement(); } catch (final Exception exception) { log.fine("Error, parsing failed, exception: " + exception.getMessage()); throw Application.createError(this, "parsing"); } if (!tag.getName().equals("memory")) { log.fine("Error, parsing failed, no <memory> tag"); throw Application.createError(this, "parsing"); } if (!MEMORY_XML_FILE_VERSION.equals(tag.getAttributeValue("version"))) { log.fine("Version mismatch"); throw Application.createError(this, "version"); } final Info info = Snapshot.processBlockElement(destinationMemory, tag, destinationAddress); if (info.number == 0) { log.fine("Reading completed, with info: number: 0"); } else { log.fine(String.format("Reading completed, with info: number: %d, min: %04x, max: %04x", info.number, info.minAddress, info.maxAddress)); } return info; }
From source file:cz.pecina.retro.trec.XML.java
License:Open Source License
/** * Reads the tape from an XML file.//from w ww. java 2s . com * * @param file input file */ public void read(final File file) { log.fine("Reading tape data from an XML file, file: " + file); try { SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema(new StreamSource( getClass().getResourceAsStream("tape-" + TAPE_XML_FILE_VERSION + ".xsd"))) .newValidator().validate(new StreamSource(file)); } catch (final Exception exception) { log.fine("Error, validation failed, exception: " + exception.getMessage()); throw Application.createError(this, "validation"); } Document doc; try { doc = new SAXBuilder().build(file); } catch (final JDOMException exception) { log.fine("Error, parsing failed, exception: " + exception.getMessage()); throw Application.createError(this, "parsing"); } catch (final Exception exception) { log.fine("Error, reading failed, exception: " + exception.getMessage()); throw Application.createError(this, "XMLRead"); } Element tag; try { tag = doc.getRootElement(); } catch (final Exception exception) { log.fine("Error, parsing failed, exception: " + exception.getMessage()); throw Application.createError(this, "parsing"); } if (!tag.getName().equals("tape")) { log.fine("Error, parsing failed, no <tape> tag"); throw Application.createError(this, "noTape"); } if (!TAPE_XML_FILE_VERSION.equals(tag.getAttributeValue("version"))) { log.fine("Version mismatch"); throw Application.createError(this, "version"); } if (!"per sec".equals(tag.getAttributeValue("unit"))) { log.fine("Unsupported sample rate"); throw Application.createError(this, "XMLSampleRate"); } tape.clear(); try { long currPos = -1; for (Element pulse : tag.getChildren("pulse")) { final long start = Long.parseLong(pulse.getAttributeValue("start")); final long duration = Long.parseLong(pulse.getAttributeValue("duration")); if ((start <= currPos) || (duration <= 0)) { log.fine("Error in XML file"); throw Application.createError(this, "XML"); } tape.put(start, duration); log.finest(String.format("Read: (%d, %d)", start, duration)); currPos = start; } } catch (final Exception exception) { log.fine("Error, parsing failed, exception: " + exception.getMessage()); throw Application.createError(this, "parsing"); } log.fine("Reading completed"); }
From source file:datalab.upo.ladonspark.controller.Loaddata.java
public List<Host> loadXml(String url, String interfaz) throws JDOMException, IOException, InterruptedException { System.out.println("\n\ncargando datos xml\n\n"); List<Host> hostfind = new ArrayList<>(); String name = ""; String ip = ""; String myIp = findMyIp(interfaz); boolean type = false; FileReader xmlFile = new FileReader(url + "nmapData.xml"); System.out.println("pasa file"); SAXBuilder builder = new SAXBuilder(); Document document = (Document) builder.build(xmlFile); Element rootNode = document.getRootElement(); /*/*w ww . j a v a 2 s . c o m*/ obtain the host list in XML file sacamos la lista de Host del fichero XML */ List<Element> hosts = rootNode.getChildren("host"); // hosts=hosts.get(0).getChildren("host"); for (int i = 0; i < hosts.size(); i++) { Element host = hosts.get(i); /* Obtain the hostname list in list host and obtain the address list in self list host */ List<Element> hostnames = host.getChildren("hostnames"); List<Element> address = host.getChildren("address"); if (hostnames.get(0).getChild("hostname") != null) { name = hostnames.get(0).getChild("hostname").getAttributeValue("name"); ip = address.get(0).getAttributeValue("addr"); } else { name = "unamed"; ip = address.get(0).getAttributeValue("addr"); } if (ip.equals(myIp)) { type = true; } else { type = false; } hostfind.add(new Host(name, ip, type)); } hostfind.remove(hostfind.get(0)); this.setHostfind(hostfind); return hostfind; }
From source file:dblp.xml.DBLPParserFirstSchema.java
public static void parse() { createDir("data/" + outputDir); DBLPParserFirstSchema parser = new DBLPParserFirstSchema(); try {/*from w w w.j a va 2 s . c om*/ title_year_writer = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(title_year_path), "utf-8")); title_conf_writer = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(title_conf_path), "utf-8")); title_author_writer = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(title_author_path), "utf-8")); SAXBuilder builder = new SAXBuilder(); String path = "data/dblp.xml"; Document jdomDocument = builder.build(path); Element root = jdomDocument.getRootElement(); // List<String> types = Arrays.asList("incollection", "article", "inproceedings", "proceedings"); List<String> types = Arrays.asList("inproceedings"); for (String type : types) { System.out.println("extractElements for " + type); parser.extractElements(root, type); } titlesCollection.writeToFile(title_path); authorsCollection.writeToFile(author_path); confsCollection.writeToFile(conf_path); yearsCollection.writeToFile(year_path); } catch (Exception e) { e.printStackTrace(); } }
From source file:dblp.xml.DBLPParserSecondSchema.java
public static void parse() { createDir("data/" + outputDir); DBLPParserSecondSchema parser = new DBLPParserSecondSchema(); try {/*ww w . j a v a 2 s . c o m*/ title_author_writer = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(title_author_path), "utf-8")); author_conf_writer = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(author_conf_path), "utf-8")); title_year_writer = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(title_year_path), "utf-8")); conf_year_writer = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(conf_year_path), "utf-8")); SAXBuilder builder = new SAXBuilder(); String path = "data/dblp.xml"; Document jdomDocument = builder.build(path); Element root = jdomDocument.getRootElement(); // List<String> types = Arrays.asList("incollection", "article", "inproceedings", "proceedings"); List<String> types = Arrays.asList("inproceedings"); for (String type : types) { parser.extractElements(root, type); } titlesCollection.writeToFile(title_path); authorsCollection.writeToFile(author_path); confsCollection.writeToFile(conf_path); yearConfsCollection.writeToFile(year_with_conf_path); yearConfsCollection.writeToFileJustFirsToken(year_path); } catch (Exception e) { e.printStackTrace(); } }
From source file:dblp.xml.DBLPParserThirdSchema.java
public static void parse() { createDir("data/" + outputDir); DBLPParserThirdSchema parser = new DBLPParserThirdSchema(); try {/*from w w w .j a v a 2 s .c o m*/ title_author_writer = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(title_author_path), "utf-8")); title_conf_writer = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(title_conf_path), "utf-8")); conf_year_writer = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(conf_year_path), "utf-8")); SAXBuilder builder = new SAXBuilder(); String path = "data/dblp.xml"; Document jdomDocument = builder.build(path); Element root = jdomDocument.getRootElement(); // List<String> types = Arrays.asList("incollection", "article", "inproceedings", "proceedings"); List<String> types = Arrays.asList("inproceedings"); for (String type : types) { parser.extractElements(root, type); } titlesCollection.writeToFile(title_path); authorsCollection.writeToFile(author_path); confsCollection.writeToFile(conf_path); yearConfsCollection.writeToFile(year_with_conf_path); yearConfsCollection.writeToFileJustFirsToken(year_path); } catch (Exception e) { e.printStackTrace(); } }