Example usage for org.jdom2.input SAXBuilder build

List of usage examples for org.jdom2.input SAXBuilder build

Introduction

In this page you can find the example usage for org.jdom2.input SAXBuilder build.

Prototype

@Override
public Document build(final String systemId) throws JDOMException, IOException 

Source Link

Document

This builds a document from the supplied URI.

Usage

From source file:jcodecollector.io.XMLManagerOldVersion.java

License:Apache License

@SuppressWarnings("unchecked")
public static ArrayList<Snippet> readPackage(File file) {
    Element root_xml = null;//from ww  w .ja v  a2 s. c  om

    try {
        SAXBuilder builder = new SAXBuilder();
        root_xml = builder.build(file).getRootElement();
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }

    ArrayList<Snippet> array = new ArrayList<Snippet>();

    Iterator<Element> iterator = root_xml.getChildren("snippet").iterator();
    while (iterator.hasNext()) {
        Element element = iterator.next();

        String category = element.getChildTextTrim("category");
        String name = element.getChildTextTrim("name");
        String syntax = element.getChildTextTrim("syntax");
        String code = element.getChildTextTrim("code");
        String comment = element.getChildTextTrim("comment");

        List<Element> tags_xml = element.getChildren("tag");
        String[] tags = new String[tags_xml.size()];
        for (int i = 0; i < tags.length; i++) {
            tags[i] = tags_xml.get(i).getTextTrim();
        }

        boolean locked = Boolean.parseBoolean(element.getChildTextTrim("locked"));

        Snippet snippet = new Snippet(-1, category, name, tags, code, comment, syntax, locked);
        array.add(snippet);
    }

    return array;
}

From source file:jdomexercises.LeerHijos.java

public static void main(String args[]) {

    try {/*from w w w  .j  ava  2 s  .c om*/
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(new FileInputStream("C:\\Users\\Alumnot\\Documents\\ej1.xml"));

        // Leer la raiz
        Element root = doc.getRootElement(); // Devuelve la raz
        System.out.println(root.getName() + ", Nombre: " + root.getAttributeValue("Nombre") + ", Ubicacion: "
                + root.getAttributeValue("Ubicacion"));

        // Creamos una lista que contendra los hijos de root
        List<Element> t = root.getChildren("Tipo");

        // Si hubiera otro hijo habra que ahcer otro getChildren()
        Iterator it = t.iterator();

        while (it.hasNext()) {
            Element e = (Element) it.next();

            Element c = e.getChild("Computadora");
            System.out.println(c.getName() + ", Nombre: " + c.getAttributeValue("Nombre") + ", Precio: "
                    + c.getAttributeValue("Precio"));

            c = e.getChild("Historieta");
            System.out.println(c.getName() + ", Nombre: " + c.getAttributeValue("Nombre") + ", Precio: "
                    + c.getAttributeValue("Precio"));

            c = e.getChild("Nivel");
            System.out.println(c.getName() + ", Nombre: " + c.getAttributeValue("Nombre"));
        }
    } catch (JDOMException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:jmap2gml.GMX.java

/**
 * Generates xml formatted room file that can be read by GMS. Uses
 * external xml file as a references and adds instances for each item.
 *
 * @param items array of items from preview
 * @param os stream to write xml string to
 *///from  w w  w. j a  va2  s  .  c o  m
public static void itemsToGMX(Item[] items, OutputStream os) {
    String prefix = String.format("inst_%d", System.currentTimeMillis());

    SAXBuilder saxBuilder = new SAXBuilder();
    int count = 0;

    try {
        Element room = saxBuilder.build(new File("template.xml")).getRootElement();
        Element instances = room.getChild("instances");

        for (Item item : items) {
            if (item != null) {
                instances.addContent(new Element("instance").setAttribute("objName", item.itemName)
                        .setAttribute("x", Integer.toString(item.x)).setAttribute("y", Integer.toString(item.y))
                        .setAttribute("name", String.format("%s%d", prefix, count++))
                        .setAttribute("locked", "0").setAttribute("code", item.creationCode)
                        .setAttribute("scaleX", Double.toString(item.xScale))
                        .setAttribute("scaleY", Double.toString(item.yScale))
                        .setAttribute("colour", "4294967295").setAttribute("rotation", "0"));
                instances.addContent("\n");
            }
        }

        XMLOutputter out = new XMLOutputter();
        out.output(room, os);

    } catch (JDOMException | IOException ex) {
    }
}

From source file:jmap2gml.GMX.java

/**
 * Creates array of Items from given gmx file.
 * Will be used from importing gms maps//from  ww w  . j av  a2 s  . com
 * 
 * @param filename .room.gmx file
 * @return array of items for each instance in the room
 */
public static Item[] gmxToItems(String filename) {
    int index = 0;
    Item[] out = null;
    String[] args;

    SAXBuilder builder = new SAXBuilder();

    try {
        Element room = builder.build(new File(filename)).getRootElement();

        out = new Item[room.getChild("instances").getChildren().size()];
        System.out.println(out.length);

        for (Element child : room.getChild("instances").getChildren()) {
            args = new String[] { child.getAttributeValue("x"), child.getAttributeValue("y"),
                    child.getAttributeValue("objName") };

            out[index] = new ItemImage(args);
            out[index].xScale = Double.parseDouble(child.getAttributeValue("scaleX"));
            out[index].yScale = Double.parseDouble(child.getAttributeValue("scaleY"));
            out[index].creationCode = child.getAttributeValue("code");

            index++;
        }

    } catch (JDOMException | IOException ex) {
    }

    return out;
}

From source file:jodtemplate.util.JDOMHelper.java

License:Apache License

public Document createJDOMDocument(final InputStream stream) throws JDOMException, IOException {
    final SAXBuilder jdomBuilder = getJDomBuilder();
    final Document jdomDocument = jdomBuilder.build(stream);
    return jdomDocument;
}

From source file:jodtemplate.util.JDOMHelper.java

License:Apache License

public Document createJDOMDocument(final String string) throws JDOMException, IOException {
    final SAXBuilder jdomBuilder = getJDomBuilder();
    final Document jdomDocument = jdomBuilder.build(new StringReader(string));
    return jdomDocument;
}

From source file:jp.co.asahi.util.Util.java

License:Apache License

/**
 * Creates a JDOM Document from a string containing XML
 *
 * @param samlRequestString String version of XML
 * @return JDOM Document if file contents converted successfully, null
 *         otherwise//from  w w  w .  j  av  a 2  s .  com
 */
public static Document createJdomDoc(String xmlString) {
    try {
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(new ByteArrayInputStream(xmlString.getBytes()));
        return doc;
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JDOMException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:jpcsp.test.NIDInfo.java

License:Open Source License

@SuppressWarnings("deprecation")
public Firmware(String version, String psplibdoc_filename) throws Exception {
    m_version = version;/*  w  ww . j a v a2 s.  co  m*/
    m_moduleCount = 0;
    m_functionCount = 0;
    m_moduleTable = new Hashtable<String, Module>();

    SAXBuilder builder = new SAXBuilder();
    Document doc = builder.build(new File(psplibdoc_filename));

    XPath modules = XPath.newInstance("/PSPLIBDOC/PRXFILES/PRXFILE/LIBRARIES/LIBRARY");

    List<?> LibList = modules.selectNodes(doc);
    //m_moduleList = modules.selectNodes(doc, "//NAME");

    Iterator<?> i = LibList.iterator();

    while (i.hasNext()) {
        Element curEl = (Element) i.next();
        String modName = curEl.getChild("NAME").getText();
        Module newMod = new Module(modName);
        List<?> FunctionList = curEl.getChild("FUNCTIONS").getChildren("FUNCTION");
        Iterator<?> j = FunctionList.iterator();
        while (j.hasNext()) {
            Element funcEl = (Element) j.next();
            newMod.addFunction(funcEl.getChild("NID").getText(), funcEl.getChild("NAME").getText());
            m_functionCount++;
        }
        m_moduleCount++;
        m_moduleTable.put(modName, newMod);
    }

    System.out.println("filename: " + psplibdoc_filename + " modules: " + m_moduleCount + " functions: "
            + m_functionCount);
}

From source file:jworkspace.ui.plaf.PlafFactory.java

License:Open Source License

/**
 * Load and install look and feels from user configuration file
 *//* ww w .  ja  va2 s  .  co  m*/
private void load() {
    String fileName = CONFIG_FILE_PATH + File.separator + sysConfig;
    LOG.info(
            WorkspaceGUI.PROMPT + "Reading file" + WorkspaceGUI.LOG_SPACE + fileName + WorkspaceGUI.LOG_FINISH);
    try {
        File file = new File(fileName);
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(file);
        Element root = doc.getRootElement();
        /*
         * Here we should find all plaf elements, described
         * in xml file under <plaf> tags
         */
        List<Element> plafs = root.getChildren("plaf");
        for (Element plaf : plafs) {
            XPlafConnector connector = XPlafConnector.create(plaf);
            if (connector != null) {
                connectors.add(connector);
            }
        }
    } catch (JDOMException | IOException e) {
        LOG.error("Cannot load plaf factory", e);
    }
}

From source file:LectorXml.CargarXML.java

public void cargarDimension(String ruta) {

    //se cra un SAXBuilder para poder parsear el archivo
    SAXBuilder builder = new SAXBuilder();
    File ArchivoXml = new File(ruta);

    try {/*from  ww w .java2  s.co  m*/
        //se crea el docuemento a traves del archio
        Document documento = (Document) builder.build(ArchivoXml);
        //se obtiene la raiz 
        Element raiz = documento.getRootElement();

        //se obtiene la lista de hijos de la raiz 
        List list = raiz.getChildren("dimension");
        //si lista tiene un elemento lo obtiene
        if (list.size() == 1) {
            System.out.println("" + raiz.getChildTextTrim("dimension"));
        } else if (list.size() == 0) {
            JOptionPane.showMessageDialog(null, "No existe ninguna dimension del tablero");
        } else {
            JOptionPane.showMessageDialog(null, "Revise su archivo de entrada");
        }

    } catch (IOException io) {
        JOptionPane.showMessageDialog(null, io.getMessage());
    } catch (JDOMException ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage());
    }
}