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:helpers.XMLParser.java

public static ParsedSingleXML parseSingleDoc(String xml) {
    ParsedSingleXML doc = null;//from w  w  w .j  a va  2 s  . co m
    try {

        SAXBuilder saxBuilder = new SAXBuilder();
        Document document = saxBuilder.build(new StringReader(xml));

        Element root = document.getRootElement();
        List<Attribute> rootAttributes = root.getAttributes();
        doc = new ParsedSingleXML(root.getName());
        for (Attribute attr : rootAttributes) {
            doc.addAttr(attr.getName(), attr.getValue());
        }

        XMLRow row;

        List<Element> rootChildren = root.getChildren();
        List<Element> tempChildren;
        List<Attribute> tempAttributes;

        for (Element child : rootChildren) {

            tempChildren = child.getChildren();
            row = new XMLRow(child.getName());
            tempAttributes = child.getAttributes();
            for (Attribute attr : tempAttributes) {
                row.addRootAttr(attr.getName(), attr.getValue());
            }

            for (Element tChild : tempChildren) {
                row.addRowElement(tChild.getName(), tChild.getValue());
            }

            doc.addRow(row);
        }

    } catch (JDOMException ex) {
        Logger.getLogger(XMLParser.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(XMLParser.class.getName()).log(Level.SEVERE, null, ex);
    }

    return doc;

}

From source file:helpers.XMLParser.java

public static LinkedList<ParsedSingleXML> parseMultipleDoc(String xml) {

    LinkedList<ParsedSingleXML> output = new LinkedList<>();
    ParsedSingleXML doc = null;/*from w ww  .  j av a 2 s. c  o m*/
    try {

        SAXBuilder saxBuilder = new SAXBuilder();
        Document document = saxBuilder.build(new StringReader(xml));

        Element root = document.getRootElement();

        List<Element> rootChildren = root.getChildren();

        for (Element child : rootChildren) {
            System.out.println(child.toString());
            output.addLast(parseSingleDoc(child.toString()));

        }

    } catch (Exception ex) {
        //return new LinkedList<>();
    }
    return output;

}

From source file:ilarkesto.integration.jdom.JDom.java

License:Open Source License

public static Document createDocument(String xmlData) {
    SAXBuilder builder = createSaxBuilder();
    try {/*www  .  j  a  v  a 2  s .c om*/
        return builder.build(new StringReader(xmlData));
    } catch (JDOMException ex) {
        throw new RuntimeException(ex);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:ilarkesto.integration.jdom.JDom.java

License:Open Source License

public static Document createDocumentFromStream(InputStream in) {
    SAXBuilder builder = createSaxBuilder();
    try {/* ww  w.j av a  2  s  .c  om*/
        return builder.build(in);
    } catch (Exception ex) {
        throw new RuntimeException("Loading XML from InputStream failed.", ex);
    }
}

From source file:ilarkesto.integration.jdom.JDom.java

License:Open Source License

public static Document createDocumentFromUrl(final String url) {
    log.debug("Loading from URL:", url);
    SAXBuilder builder = createSaxBuilder();
    try {/*from  w  w w  . jav a2 s .  co m*/
        return builder.build(new URL(url));
    } catch (Exception ex) {
        throw new RuntimeException("Loading XML from URL failed: " + url, ex);
    }
}

From source file:ilarkesto.integration.jdom.JDom.java

License:Open Source License

public static Document createDocumentFromUrl(String url, String username, String password) {
    log.debug("Downloading:", url);
    SAXBuilder builder = createSaxBuilder();
    try {//from  ww  w.  ja va  2s. c o  m
        BufferedInputStream is = new BufferedInputStream(IO.openUrlInputStream(url, username, password));
        Document doc = builder.build(is);
        IO.closeQuiet(is);
        return doc;
    } catch (Exception ex) {
        throw new RuntimeException("Loading XML from URL failed: " + url, ex);
    }
}

From source file:information.Information.java

License:Open Source License

private static void leerDatos() {
    java.io.File archivo = new java.io.File(informationPath);
    SAXBuilder saxBuilder = new SAXBuilder();
    try {/*from   ww  w  . j  a va  2 s.  c o  m*/
        Document documento = saxBuilder.build(archivo);
        Element raiz = documento.getRootElement();
        List<Element> elementosSalon = raiz.getChildren("Salon");
        for (Element salone : elementosSalon) {
            Salon salon = new Salon(salone.getAttributeValue("Nombre"));
            List<Element> elementosSala = salone.getChildren("Sala");
            for (Element salae : elementosSala) {
                Room sala = new Room(salae.getAttributeValue("Nombre"),
                        Boolean.parseBoolean(salae.getAttributeValue("Horizontal")),
                        Integer.parseInt(salae.getAttributeValue("SufijoIP")));
                List<Element> elementosFila = salae.getChildren("Fila");
                for (Element filae : elementosFila) {
                    Row fila = new Row(sala.isHorizontal());
                    List<Element> elementosEquipo = filae.getChildren("Equipo");
                    for (Element equipoe : elementosEquipo) {
                        ServerComputer equipo = new ServerComputer(sala);
                        equipo.setComputerNumber(Integer.parseInt(equipoe.getAttributeValue("Numero")));
                        equipo.setIP(equipoe.getChildText("IP"));
                        equipo.setMac(equipoe.getChildText("Mac"));
                        equipo.setHostname(equipoe.getChildText("Hostname"));
                        List<Element> elementosEjecucion = equipoe.getChildren("Ejecucion");
                        for (Element ejecucione : elementosEjecucion) {
                            String[] resultado = { ejecucione.getAttributeValue("Orden"),
                                    ejecucione.getAttributeValue("Resultado") };
                            equipo.addResult(resultado);
                        }
                        fila.addComputer(equipo);
                    }
                    sala.addRow(fila);
                }
                salon.addRoom(sala);
            }
            salons.add(salon);
        }
    } catch (JDOMException ex) {
        Logger.getLogger(Information.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Information.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:insa.h4401.utils.DocumentFactory.java

/**
 * Creates a MapDocument object from the given File object
 *
 * @param file file to interpret as a MapDocument
 * @return null if the given file doesn't exists or is a directory
 *///  w  ww. j  a  v a  2 s  .co m
public static MapDocument createMapDocument(File file) {
    // Reset factory error
    DocumentFactory.error = null;
    // Create MapDocument
    MapDocument mapdoc = null;
    if (file.exists() && !file.isDirectory()) {
        SAXBuilder builder = new SAXBuilder();
        try {
            Document document = builder.build(file);
            mapdoc = new MapDocument(document);
        } catch (IOException | JDOMException ex) {
            DocumentFactory.error = "Invalid XML file";
        }
    } else {
        DocumentFactory.error = "File is not a regular file";
    }
    return mapdoc;
}

From source file:insa.h4401.utils.DocumentFactory.java

/**
 * Creates a PlanningDocument object from the given File object
 *
 * @param file file to interpret as a PlanningDocument
 * @return a planningDocument//from   w w  w . j ava2s  .  c om
 */
public static PlanningDocument createPlanningDocument(File file) {
    // Reset factory error
    DocumentFactory.error = null;
    // Create a PlanningDocument
    PlanningDocument plandoc = null;
    if (file.exists() && !file.isDirectory()) {
        SAXBuilder builder = new SAXBuilder();
        try {
            Document document = builder.build(file);
            plandoc = new PlanningDocument(document);
        } catch (IOException | JDOMException ex) {
            DocumentFactory.error = "Invalid XML file";
        }
    } else {
        DocumentFactory.error = "File is not a regular file";
    }
    return plandoc;
}

From source file:instanceXMLParser.Instance.java

public void buildINstanceJDom(String path) {
    //creating JDOM SAX parser
    SAXBuilder builder = new SAXBuilder();

    //reading XML document
    Document xml = null;// ww  w .jav  a  2  s. c  o m
    try {
        xml = builder.build(new File(path));
    } catch (JDOMException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    //getting root element from XML document
    Element root = xml.getRootElement();
    List<Element> list = root.getChildren();
    for (Element element : list) {
        List<Element> list1;
        if (element.getName().equals("board")) {
            list1 = element.getChildren();
            for (Element element2 : list1) {
                if (element2.getName().equals("size_n")) {//size of the space
                    size_n = Integer.parseInt(element2.getText());
                } else if (element2.getName().equals("size_m")) {//size of the space
                    size_m = Integer.parseInt(element2.getText());
                    //inizializzo matrice solo dop aver letto le due dimensioni
                    //NOTA CHE SIZE_M E SIZE_N devono essere i primi elementi e in quell ordine!
                    boardState = new CellLogicalState[size_n][size_m];
                    for (int j = 0; j < size_n; j++) {
                        for (int k = 0; k < size_m; k++) {
                            boardState[j][k] = new CellLogicalState();
                        }
                    }

                } else if (element2.getName().equals("tile_state")) {//tile states
                    int x, y, value;
                    CellLogicalState state = new CellLogicalState();
                    String stateString;
                    x = Integer.parseInt(element2.getAttribute("x").getValue());
                    y = Integer.parseInt(element2.getAttribute("y").getValue());

                    stateString = element2.getText();

                    if (stateString.equals("obstacle")) {
                        state.setLocState(LocationState.Obstacle);
                    } else if (stateString.equals("dirty")) {
                        state.setLocState(LocationState.Dirty);
                        value = 1;
                        if (element2.getAttribute("value").getValue() != null) {
                            value = Integer.parseInt(element2.getAttribute("value").getValue());
                        }
                        state.setDirtyAmount(value);

                    }

                    boardState[x][y] = state;

                }
            }
        } else if (element.getName().equals("agent")) {//agent
            List<Element> list3 = element.getChildren();
            for (Element element3 : list3) {
                if (element3.getName().equals("x")) {
                    agentPos.setX(Integer.parseInt(element3.getValue()));
                } else if (element3.getName().equals("y")) {
                    agentPos.setY(Integer.parseInt(element3.getValue()));
                } else if (element3.getName().equals("energy")) {
                    energy = Double.parseDouble(element3.getValue());
                }
            }
        } else if (element.getName().equals("base")) {//agent
            List<Element> list3 = element.getChildren();
            for (Element element3 : list3) {
                if (element3.getName().equals("x")) {
                    basePos.setX(Integer.parseInt(element3.getValue()));
                } else if (element3.getName().equals("y")) {
                    basePos.setY(Integer.parseInt(element3.getValue()));
                }
            }
        } else if (element.getName().equals("action_costs")) {//agent
            List<Element> list3 = element.getChildren();
            for (Element element3 : list3) {
                if (element3.getName().equals("up") || element3.getName().equals("left")
                        || element3.getName().equals("down") || element3.getName().equals("right")
                        || element3.getName().equals("suck")) {

                    actionCosts.put(element3.getName(), Double.parseDouble(element3.getValue()));

                }
            }
        }

    }
}