Example usage for org.jdom2 Document getRootElement

List of usage examples for org.jdom2 Document getRootElement

Introduction

In this page you can find the example usage for org.jdom2 Document getRootElement.

Prototype

public Element getRootElement() 

Source Link

Document

This will return the root Element for this Document

Usage

From source file:helpers.XMLParser.java

public static ParsedSingleXML parseSingleDoc(String xml) {
    ParsedSingleXML doc = null;/*  w  w w  .  j a  v  a 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  w  w  .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:hintahaku.Asetukset.java

public static void avaa() {
    Document xml;
    try (BufferedReader reader = Files.newBufferedReader(tiedosto)) {
        xml = new SAXBuilder().build(reader);
    } catch (JDOMException | IOException ex) {
        return;//from  w w  w. ja v  a2 s . co m
    }
    Element juuri = xml.getRootElement();

    if (!"Hintahaku-asetukset".equals(juuri.getAttributeValue("info"))) {
        return;
    }

    try {
        suodatintiedosto = Paths.get(juuri.getChildText("suodatintiedosto"));
    } catch (InvalidPathException | NullPointerException ex) {
    }
    try {
        viimeisinSuodatinKansio = Paths.get(juuri.getChildText("viimeisinSuodatinKansio"));
    } catch (InvalidPathException | NullPointerException ex) {
    }
    try {
        viimeisinKokoonpanoKansio = Paths.get(juuri.getChildText("viimeisinKokoonpanoKansio"));
    } catch (InvalidPathException | NullPointerException ex) {
    }
}

From source file:hintahaku.Suodattimet.java

public static void vaihda(Path tiedosto) throws IOException, JDOMException, SuodatinTiedostoException {
    Document xml;
    try (BufferedReader reader = Files.newBufferedReader(tiedosto)) {
        xml = new SAXBuilder().build(reader);
    }//from www  .  ja va2s .  c o m
    Element juuri = xml.getRootElement();

    if (!"Hintahaku-suodattimet".equals(juuri.getAttributeValue("info"))) {
        throw new SuodatinTiedostoException();
    }

    tapahtumatPaalla = false;

    Set<Kauppa> tiedostossa = new HashSet<>();

    for (Element kauppaxml : juuri.getChildren()) {
        String kaupanNimi = kauppaxml.getChildText("nimi");
        if (kaupanNimi != null) {
            boolean voiNoutaa = Boolean.parseBoolean(kauppaxml.getChildText("voiNoutaa"));
            boolean suodataPois = Boolean.parseBoolean(kauppaxml.getChildText("suodataPois"));

            Kauppa haettuKauppa = haeKauppa(kaupanNimi);
            if (haettuKauppa == null) {
                Kauppa uusiKauppa = new Kauppa(kaupanNimi, voiNoutaa, suodataPois);
                lisaaKauppa(uusiKauppa);
                tiedostossa.add(uusiKauppa);
            } else {
                haettuKauppa.setVoiNoutaa(voiNoutaa);
                haettuKauppa.setSuodataPois(suodataPois);
                tiedostossa.add(haettuKauppa);
            }
        }
    }

    for (Kauppa kauppa : kaupatEventList) {
        if (!tiedostossa.contains(kauppa)) {
            kauppa.setVoiNoutaa(false);
            kauppa.setSuodataPois(false);
        }
    }

    tapahtumatPaalla = true;
    pcs.firePropertyChange("suodattimet", null, null);

    Suodattimet.tiedosto = tiedosto;
}

From source file:ids.SettingsFrame.java

private void SaveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveButtonActionPerformed

    String AdminMail = AdminMailField.getText();
    SpinnerModel spinmodel = FitnessSpinner.getModel();
    Integer FitnessValue;//from  w ww. j  av a  2 s.c  o  m
    try {
        FitnessValue = (Integer) spinmodel.getValue();
    } catch (Exception ex) {
        IDS.msg.ErrorMessages("Only Integers Accepted ");
        return;
    }
    if (AdminMail.equals("")) {
        IDS.msg.ErrorMessages("Please Input the Admin Email");
        return;
    } else if (!AdminMail.contains("@")) {
        IDS.msg.ErrorMessages("Please Input a Valid Admin Email");
        return;
    } else if (FitnessValue > 100 || FitnessValue < 0) {
        IDS.msg.ErrorMessages("Please Select A Trigger Fitness % Between 0 and 100%");
        return;
    }

    int PortFilterIndex = FilterComboBox.getSelectedIndex();
    String PortFilterIndexString = String.valueOf(PortFilterIndex);
    String arp = "false", icmp = "false", igmp = "false", ip = "false", tcp = "false", udp = "false",
            all = "false";
    if (ARPCheckBox.isSelected() == true) {
        arp = "true";
    }
    if (ICMPCheckBox.isSelected() == true) {
        icmp = "true";
    }
    if (IGMPCheckBox.isSelected() == true) {
        igmp = "true";
    }
    if (IPCheckBox.isSelected() == true) {
        ip = "true";
    }
    if (TCPCheckBox.isSelected() == true) {
        tcp = "true";
    }
    if (UDPCheckBox.isSelected() == true) {
        udp = "true";
    }
    if (FilterAllCheckBox.isSelected() == true) {
        all = "true";
    }
    String SpinnerValue = String.valueOf((int) FitnessValue);
    String[] ToSave = { AdminMail, SpinnerValue, PortFilterIndexString, arp, icmp, igmp, ip, tcp, udp, all };

    //Generate port filter string
    if (Integer.parseInt(ToSave[2]) == 0) {
        IDS.ActivatePortFilter = false;
    } else {
        IDS.ActivatePortFilter = true;
        String[] get = FilterComboBox.getSelectedItem().toString().split(" ");
        IDS.PortFilterString = get[2] + " " + get[3];
        synchronized (IDS.print) {
            IDS.print.TextAreaAppend(IDS.PortFilterString + " Filtering Activated\n");

        }
    }

    //Generate Protocol Filter String
    String[] Protocols = { "arp", "icmp", "igmp", "ip", "tcp", "udp" };
    String ProtoFilterString = "";
    if (Boolean.parseBoolean(ToSave[9]) == true) {
        IDS.ActivateProtocolFilter = true;
    } else {
        for (int i = 3; i < 9; i++) {

            switch (ToSave[i]) {
            case "true":
                ProtoFilterString = ProtoFilterString + Protocols[i - 3] + " or ";
                break;
            case "false":

                break;

            }
        }
        IDS.ProtoFilterString = ProtoFilterString.substring(0, ProtoFilterString.length() - 4);
        synchronized (IDS.print) {
            IDS.print.TextAreaAppend(IDS.ProtoFilterString + " Protocol Filtering Activated\n");
        }
    }

    //Write New Settings to XML File in memory
    try {
        //IDS.InputFile = new File("settings.xml");
        //IDS.saxBuilder = new SAXBuilder();
        org.jdom2.Document document = IDS.saxBuilder.build(IDS.InputFile);
        org.jdom2.Element rootElement = document.getRootElement();
        //get preferencecss
        org.jdom2.Element UserPreferences = rootElement.getChild("settings");
        // loop the preferences child node
        for (int i = 0; i < 10; i++) {
            org.jdom2.Element IndividualElement = UserPreferences.getChild(ElementNames[i]);
            IndividualElement.setText(ToSave[i]);
        }

        //Write to file in real life
        FileOutputStream fileout = new FileOutputStream("settings.xml");
        IDS.xmlout.output((org.jdom2.Document) document, fileout);
        fileout.flush();
        fileout.close();
        synchronized (IDS.msg) {

            IDS.msg.InformationMessages(
                    "Save Successfull. Kindly Restart Sniffing if You are Already Doing So");
        }
        this.dispose();
    } catch (Exception ex) {
        synchronized (IDS.msg) {
            IDS.msg.ErrorMessages("Error: " + ex);
        }
    }

}

From source file:ilarkesto.feeds.Feed.java

License:Open Source License

public static Feed load(String url) {
    Document document = JDom.createDocumentFromUrl(url);
    Element eRoot = document.getRootElement();
    Element eChannel = eRoot.getChild("channel");

    String title = JDom.getChildText(eChannel, "title");
    String link = JDom.getChildText(eChannel, "link");
    String description = JDom.getChildText(eChannel, "description");
    Feed feed = new Feed(title, link, description);
    feed.setLastBuildDate(JDom.getChildText(eChannel, "lastBuildDate"));
    feed.setPubDate(JDom.getChildText(eChannel, "lastBuildDate"));
    feed.setLanguage(JDom.getChildText(eChannel, "language"));

    for (Element eItem : eChannel.getChildren("item")) {
        String itemTitle = JDom.getChildText(eItem, "title");
        String itemDescription = JDom.getChildText(eItem, "description");
        FeedItem item = new FeedItem(itemTitle, itemDescription);
        feed.addItem(item);/*  w  w w  .  jav  a2  s  . c  om*/
        item.setLink(JDom.getChildText(eItem, "link"));
        item.setPubDate(JDom.getChildText(eItem, "pubDate"));
        item.setGuid(JDom.getChildText(eItem, "guid"));
    }

    return feed;
}

From source file:ilarkesto.integration.google.Google.java

License:Open Source License

public static String weatherInfo(ilarkesto.core.time.Date date, String language, String location) {
    if (date.isPast())
        return null;
    int inDays = date.getPeriodTo(ilarkesto.core.time.Date.today()).abs().toDays();
    if (inDays > 4)
        return null;

    Document doc = weatherAsXml(language, location);
    Element eReply = doc.getRootElement();
    Element eWeather = eReply.getChild("weather");
    if (eWeather == null)
        return null;

    if (date.isToday()) {
        Element eConditions = eWeather.getChild("current_conditions");
        if (eConditions == null)
            return null;
        String temp = JDom.getChildAttributeValue(eConditions, "temp_c", "data");
        String condition = JDom.getChildAttributeValue(eConditions, "condition", "data");
        String humidity = JDom.getChildAttributeValue(eConditions, "humidity", "data");
        String wind = JDom.getChildAttributeValue(eConditions, "wind_condition", "data");
        StringBuilder sb = new StringBuilder();
        sb.append(temp).append(", ").append(condition);
        if (humidity != null)
            sb.append(", ").append(humidity);
        if (wind != null)
            sb.append(", ").append(wind);
        return sb.toString();
    }//from  w w w .j  ava2 s . c  o  m

    List<Element> elConditions = JDom.getChildren(eWeather, "forecast_conditions");
    if (elConditions.isEmpty())
        return null;
    if (elConditions.size() < inDays)
        return null;
    Element eConditions = elConditions.get(inDays - 1);

    return JDom.getChildAttributeValue(eConditions, "condition", "data");
}

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

License:Open Source License

public static Element getChild(Document doc, String name) {
    return getChild(doc.getRootElement(), name);
}

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

License:Open Source License

public static String getChildAttributeValue(Document doc, String childName, String attributeName) {
    return getChildAttributeValue(doc.getRootElement(), childName, attributeName);
}

From source file:ilarkesto.tools.TatbestandskatalogParser.java

License:Open Source License

public TatbestandskatalogParser(File file) throws Exception {
    Document document = JDom.createDocument(file);
    Element eRoot = document.getRootElement();

    List<Element> ePages = JDom.getChildren(eRoot, "page");
    System.out.println(ePages.size() + " pages");

    // processPage(ePages.get(54 - 1));
    // processPage(ePages.get(70 - 1));
    // if (true) return;

    List<Tatbestand> tatbestaende = new ArrayList<Tatbestandskatalog.Tatbestand>();
    for (int i = 19; i <= 474; i++) {
        try {//from  www. jav  a  2s. co  m
            tatbestaende.addAll(processPage(ePages.get(i - 1)));
        } catch (Exception ex) {
            throw new RuntimeException("Parsing page " + i + " failed.", ex);
        }
    }

    Tatbestandskatalog tatbestandskatalog = new Tatbestandskatalog(tatbestaende);

    Tatbestand tb1 = tatbestandskatalog.getTatbestand(101000);
    if (tb1 == null)
        throw new RuntimeException("Tatbestand fehlt: 101000");
    if (!tb1.getFapPkt().equals("(B - 0)"))
        throw new RuntimeException(tb1.toString());
    if (tb1.getEuro() != 3500)
        throw new RuntimeException(tb1.toString());
    if (tb1.getFv() != null)
        throw new RuntimeException(tb1.toString());

    Tatbestand tb2 = tatbestandskatalog.getTatbestand(103851);
    if (tb2 == null)
        throw new RuntimeException("Tatbestand fehlt: 103851");
    if (!tb2.getFapPkt().equals("A - 2"))
        throw new RuntimeException(tb2.toString());
    if (tb2.getEuro() != 44000)
        throw new RuntimeException(tb2.toString());
    if (tb2.getFv() != 2)
        throw new RuntimeException(tb2.toString());

    assertExists(103815, tatbestandskatalog);
    assertExists(105131, tatbestandskatalog);
    assertExists(113102, tatbestandskatalog);

    System.out.println(tatbestandskatalog.getJson().toFormatedString());
    File outputFile = new File("runtimedata/tatbestandskatalog.json").getAbsoluteFile();
    System.out.println(outputFile);
    tatbestandskatalog.getJson().write(outputFile, true);
}