Example usage for org.jdom2.input SAXBuilder SAXBuilder

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

Introduction

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

Prototype

public SAXBuilder() 

Source Link

Document

Creates a new JAXP-based SAXBuilder.

Usage

From source file:jcodecollector.io.XMLManagerOldVersion.java

License:Apache License

public static void importFromXML(File file) throws IOException, JDOMException {
    Element root = new SAXBuilder().build(file).getRootElement();

    // ArrayList<Syntax> syntaxes = getSyntaxesFromFile(root);
    ArrayList<Snippet> snippets = getSnippetsFromFile(root);

    // for (Syntax s : syntaxes) {
    // DBMS.getInstance().insertNewSyntax(s);
    // }//from w ww. ja  v  a2 s . com

    // inserisco gli snippet
    for (Snippet s : snippets) {
        DBMS.getInstance().insertNewSnippet(s);
    }
}

From source file:jdomexercises.LeerHijos.java

public static void main(String args[]) {

    try {// ww  w .  j a  v a2  s.  co  m
        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:jgoose.IEC61850_GOOSE_ICD_file.java

License:Open Source License

public IEC61850_GOOSE_ICD_file(String icd_filename, String ied_name)
        throws JDOMException, IOException, IEC61850_GOOSE_Exception {
    boolean found_ConnectedAP = false;
    short found_Addressing_data = 0;
    boolean found_IED = false;

    xml_builder = new SAXBuilder();
    xml_file = new File(icd_filename);
    xml_document = (Document) xml_builder.build(xml_file);

    // Checks that the XML file is indeed an SCL file
    if (xml_document.getRootElement().getName() != "SCL")
        throw new IEC61850_GOOSE_Exception("XML input file is not of SCL type");

    root_namespace = xml_document.getRootElement().getNamespace();

    // Retrieve Node Communication
    Element comm_element = xml_document.getRootElement().getChild("Communication", root_namespace);

    // Retrieves all elements named "ConnectedAP" within Communication node
    Filter<Element> elementFilter = new ElementFilter("ConnectedAP");

    // Search for a ConnectedAP with a matching iedName
    for (Iterator<Element> ConnectedAP_IT = comm_element.getDescendants(elementFilter); ConnectedAP_IT
            .hasNext();) {//from ww w. j  av a 2  s.  c  om
        Element current_element = ConnectedAP_IT.next();

        if (current_element.getAttributeValue("iedName").equals(ied_name)) {
            // We found the ConnectedAP node with the correct iedName
            found_ConnectedAP = true;

            iedName = current_element.getAttributeValue("iedName");

            ConnectedAP_in_Comm_section = current_element;
            apName = ConnectedAP_in_Comm_section.getAttributeValue("apName");

            // walks to the "Address" children 
            Element ConnectedAP_in_Comm_section_Address = ConnectedAP_in_Comm_section.getChild("Address",
                    root_namespace);
            List<Element> p_nodes_LIST = ConnectedAP_in_Comm_section_Address.getChildren("P", root_namespace);

            // Walks all P nodes to retrieve addressing data
            for (int position = 0; position < p_nodes_LIST.size(); position++) {
                if (p_nodes_LIST.get(position).getAttributeValue("type").contentEquals("IP")) {
                    ipAddress = p_nodes_LIST.get(position).getValue();
                    found_Addressing_data |= 1;
                } else if (p_nodes_LIST.get(position).getAttributeValue("type").contentEquals("IP-SUBNET")) {
                    ipSubnet = p_nodes_LIST.get(position).getValue();
                    found_Addressing_data |= 2;
                } else if (p_nodes_LIST.get(position).getAttributeValue("type").contentEquals("IP-GATEWAY")) {
                    ipGateway = p_nodes_LIST.get(position).getValue();
                    found_Addressing_data |= 4;
                } else if (p_nodes_LIST.get(position).getAttributeValue("type").contentEquals("MAC-Address")) {
                    macAddress = p_nodes_LIST.get(position).getValue();
                    found_Addressing_data |= 8;
                }
            }

            // If all 4 addressing data fields were not found, we rise an exception.
            if (found_Addressing_data != 15)
                throw new IEC61850_GOOSE_Exception("Missing addressing data in SCL file: " + icd_filename
                        + " iedName: " + iedName + " apName: " + apName);
        }
    }

    if (found_ConnectedAP == false)
        throw new IEC61850_GOOSE_Exception("ConnectedAP section with corresponding IED name: " + iedName
                + " not found in SCL file: " + icd_filename);

    // Now we have to find the IED and the Access Point in the <IED> section
    // Retrieve IED Children
    List<Element> ied_nodes_LIST = xml_document.getRootElement().getChildren("IED", root_namespace);
    for (int position = 0; position < ied_nodes_LIST.size(); position++) {
        if (ied_nodes_LIST.get(position).getAttributeValue("name").equals(ied_name)
                && ied_nodes_LIST.get(position).getChild("AccessPoint", root_namespace)
                        .getAttributeValue("name").equals(apName)) {
            IED_section = ied_nodes_LIST.get(position);
            found_IED = true;
        }
    }

    if (found_IED == false)
        throw new IEC61850_GOOSE_Exception("IED section with corresponding AccessPoint name: " + apName
                + " not found in SCL file: " + icd_filename);
}

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
 *///w w  w. jav  a  2 s.  c om
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 w  w  w .j  a v  a 2s.c om
 * 
 * @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

private SAXBuilder getJDomBuilder() {
    final SAXBuilder jdomBuilder = new SAXBuilder();
    jdomBuilder.setJDOMFactory(new SlimJDOMFactory());
    jdomBuilder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    return jdomBuilder;
}

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 www.  java 2  s . c om*/
 */
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;
}