Example usage for org.dom4j Node selectSingleNode

List of usage examples for org.dom4j Node selectSingleNode

Introduction

In this page you can find the example usage for org.dom4j Node selectSingleNode.

Prototype

Node selectSingleNode(String xpathExpression);

Source Link

Document

selectSingleNode evaluates an XPath expression and returns the result as a single Node instance.

Usage

From source file:com.weibo.datasys.crawler.base.factory.TaskFactory.java

License:Open Source License

/**
 * /*from   w ww.  j  a  v  a 2 s . co  m*/
 * ???node??Map
 * 
 * @param node
 * @return
 */
@SuppressWarnings("unchecked")
private static Map<String, String> getParaMap(Node node) {
    Map<String, String> paraMap = new LinkedHashMap<String, String>();
    if (node != null) {
        List<Node> paraNodes = node.selectNodes("parameter");
        for (Node paraNode : paraNodes) {
            String key = paraNode.selectSingleNode("@key").getText().trim();
            String value = paraNode.getText().trim();
            Node valueNode = paraNode.selectSingleNode("@value");
            if (valueNode != null) {
                value = paraNode.selectSingleNode("@value").getText().trim();
            }
            paraMap.put(key, value);
        }
    }
    return paraMap;
}

From source file:com.xebia.mojo.dashboard.util.TidyXmlUtil.java

License:Apache License

/** {@inheritDoc} */
public Node findNode(Node node, String xpath) {
    return node.selectSingleNode(xpath);
}

From source file:controler.ImportDataFromXML.java

public ImportDataFromXML() throws UnsupportedEncodingException, IOException, DocumentException {
    //ArrayList<Password> registos = new ArrayList<>();
    //Password reg;

    File fis = new File("cyphDados.xml");
    SAXReader reader = new SAXReader();
    Document document = reader.read(fis);

    Element classElement = document.getRootElement();// why??

    List<Node> nodes = document.selectNodes("/Registos/Registo");

    for (Node node : nodes) {
        Password reg = new Password();

        reg.setTitle(node.selectSingleNode("titulo").getText());
        reg.setUser(node.selectSingleNode("user").getText());
        reg.setPass(node.selectSingleNode("pass").getText());
        reg.setSite(node.selectSingleNode("site").getText());
        reg.setNote(node.selectSingleNode("nota").getText());

        ApplicationData.getAppData().addPasswords(reg);
    }/*from   w  ww . ja  v  a 2  s .  c o m*/

}

From source file:data.BookshareWS.java

@Override
public void parse(String tekst) throws Exception {
    try {/*from  w  w  w . ja va 2 s  .com*/
        //List<Book> lista=new ArrayList<>();
        Document document = DocumentHelper.parseText(tekst);

        List list = document.selectNodes("//result");
        //System.out.println("Velicina liste "+list.size());
        if (list == null || list.size() == 0) {
            System.out.println("Lista je null");
            kraj = true;
        } else {
            System.out.println("Trenutni broj strana " + trenutnaStrana);
            for (int i = 0; i < list.size(); i++) {
                Book b = new Book();
                b.setUri(URIGenerator.generateUri(b));
                Node node = (Node) list.get(i);
                b.setTitle(node.selectSingleNode("title").getText());

                Node isbn = node.selectSingleNode("isbn13");
                if (isbn != null) {
                    b.setIsbn(isbn.getText());
                }
                List autori = node.selectNodes("author");

                for (int j = 0; j < autori.size(); j++) {
                    Person p = new Person();
                    p.setUri(URIGenerator.generateUri(p));
                    p.setName(((Node) autori.get(j)).getText());
                    b.getAuthors().add(p);
                }

                Organization o = new Organization();
                o.setUri(URIGenerator.generateUri(o));
                Node publisher = node.selectSingleNode("publisher");
                if (publisher != null) {
                    o.setName(publisher.getText());
                }
                b.setPublisher(o);

                Node description = node.selectSingleNode("brief-synopsis");
                if (description != null) {
                    b.setDescription(description.getText());
                }
                lista.add(b);

            }
            trenutnaStrana++;
            //kraj=true;
        }

    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
}

From source file:data.IsbndbWS.java

@Override
public void parse(String tekst) throws Exception {
    try {//from   w w w  .j av a 2s .  c om

        //List<Book> lista=new ArrayList<>();

        Document document = DocumentHelper.parseText(tekst);

        List list = document.selectNodes("//BookData");

        if (list == null || list.size() == 0) {
            System.out.println("Doslo do kraja");
            kraj = true;
        } else {
            System.out.println("ISBNDB, Trenutni broj strane " + trenutniBrojStrane);
            for (int i = 0; i < list.size(); i++) {
                Book b = new Book();
                b.setUri(URIGenerator.generateUri(b));
                Node node = (Node) list.get(i);
                b.setTitle(node.selectSingleNode("Title").getText());
                b.setIsbn(node.valueOf("@isbn13"));
                Person p = new Person();
                p.setUri(URIGenerator.generateUri(p));
                p.setName(node.selectSingleNode("AuthorsText").getText());
                b.getAuthors().add(p);
                Organization o = new Organization();
                o.setUri(URIGenerator.generateUri(o));
                o.setName(node.selectSingleNode("PublisherText").getText());
                b.setPublisher(o);

                lista.add(b);

            }

            trenutniBrojStrane++;
            //kraj=true;
        }

    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
}

From source file:datasource.Dispatcher.java

License:Open Source License

public void run() {
    Iterator<Node> nodeIterator = config.selectNodes("/pages/page[@mode='SYMBOL']").listIterator();
    while (nodeIterator.hasNext()) {
        Node n = nodeIterator.next();

        PageParameterSet pageParameterSet = new PageParameterSet();

        String pattern = n.selectSingleNode("@datetimepattern").getText();
        pageParameterSet.dateTimeFormatter = DateTimeFormat.forPattern(pattern);

        pattern = n.selectSingleNode("@decimalpattern").getText();
        pageParameterSet.decimalFormat = (DecimalFormat) DecimalFormat.getInstance();
        pageParameterSet.decimalFormat.applyPattern(pattern);

        if (n.selectSingleNode("@mode").getText().equals("SYMBOL")) {
            pageParameterSet.mode = PageParameterSet.MODE_SYMBOL;
            System.out.println("set mode to symbol");
        } else {//w  w  w. j  a  v a2 s  .co  m
            pageParameterSet.mode = PageParameterSet.MODE_ISIN;
            System.out.println("set mode to isin");
        }
        pageParameterSet.url = n.selectSingleNode("@url").getText();

        pageParameterSet.priceXPath = n.selectSingleNode("trigger[@name='price']/@xpath").getText();
        pageParameterSet.pricePattern = n.selectSingleNode("trigger[@name='price']/@regex").getText();

        pageParameterSet.valutaXPath = n.selectSingleNode("trigger[@name='valuta']/@xpath").getText();
        pageParameterSet.valutaPattern = n.selectSingleNode("trigger[@name='valuta']/@regex").getText();

        pageParameterSet.currencyXPath = n.selectSingleNode("trigger[@name='currency']/@xpath").getText();
        pageParameterSet.alternateCurrencyXPath = n.selectSingleNode("trigger[@name='currencyA']/@xpath")
                .getText();
        pageParameterSet.currencyPattern = n.selectSingleNode("trigger[@name='currency']/@regex").getText();

        while (commodityIterator.hasNext()) {
            PriceDataSource pds = new PriceDataSource(priceHashMap, pageParameterSet, commodityIterator.next());
            //threadVector.add(pds);
            pds.run();
        }
    }
}

From source file:de.thischwa.pmcms.model.tool.BackupParser_1.java

License:LGPL

@Override
public void run() throws Exception {
    int pageCount = 0;

    // counting elements
    pageCount += root.selectNodes("//page", ".").size();
    pageCount += root.selectNodes("//gallery", ".").size();
    pageCount += root.selectNodes("//image", ".").size();
    if (monitor != null)
        monitor.beginTask(LabelHolder.get("task.site.backup.read.monitor").concat(String.valueOf(pageCount)),
                pageCount);//  ww w  . j  a  v a2 s . c  om

    Element transfer = (Element) root.selectSingleNode("transfer");
    site = new Site();
    site.setUrl(root.attributeValue("url"));
    site.setTitle(root.selectSingleNode("title").getText());
    site.setTransferHost(transfer.attributeValue("host"));
    site.setTransferLoginUser(transfer.attributeValue("user"));
    site.setTransferLoginPassword(transfer.attributeValue("password"));
    site.setTransferStartDirectory(transfer.attributeValue("startdir"));

    @SuppressWarnings("unchecked")
    List<Node> macros = root.selectNodes("macro");
    for (Node node : macros) {
        Macro macro = new Macro();
        macro.setName(node.selectSingleNode("name").getText());
        macro.setText(contentFixes(node.selectSingleNode("text").getText()));
        macro.setParent(site);
        site.add(macro);
    }

    @SuppressWarnings("unchecked")
    List<Node> nodes = root.selectNodes("template");
    for (Node node : nodes) {
        Template template = new Template();
        template.setId(Integer.parseInt(((Element) node).attributeValue("id")));
        template.setType(TemplateType.getType(((Element) node).attributeValue("type")));
        template.setName(node.selectSingleNode("name").getText());
        template.setText(contentFixes(node.selectSingleNode("text").getText()));
        template.setParent(site);
        if (template.isLayoutTemplate())
            site.setLayoutTemplate(template);
        else {
            site.add(template);
            templateCache.put(template.getId(), template);
        }
    }

    Element welcomePage = (Element) root.selectSingleNode("page");
    if (welcomePage != null) {
        importPage(site, welcomePage);
    } else {
        welcomePage = (Element) root.selectSingleNode("gallery");
        if (welcomePage != null)
            importGallery(site, welcomePage);
    }

    @SuppressWarnings("unchecked")
    List<Node> levels = root.selectNodes("level");
    for (Node node : levels) {
        importLevel(site, node);
    }
}

From source file:de.thischwa.pmcms.model.tool.BackupParser_old.java

License:LGPL

@Override
public void run() throws Exception {
    int pageCount = 0;

    // counting elements
    pageCount += root.selectNodes("//page", ".").size();
    pageCount += root.selectNodes("//gallery", ".").size();
    pageCount += root.selectNodes("//image", ".").size();
    if (monitor != null)
        monitor.beginTask(LabelHolder.get("task.site.backup.read.monitor").concat(String.valueOf(pageCount)),
                pageCount);//from  w  ww .j av a  2 s.c om

    Element transfer = (Element) root.selectSingleNode("transfer");
    site = new Site();
    site.setUrl(root.attributeValue("url"));
    site.setTitle(root.selectSingleNode("title").getText());
    site.setTransferHost(transfer.attributeValue("host"));
    site.setTransferLoginUser(transfer.attributeValue("user"));
    site.setTransferLoginPassword(transfer.attributeValue("password"));
    site.setTransferStartDirectory(transfer.attributeValue("startdir"));

    @SuppressWarnings("unchecked")
    List<Node> macros = root.selectNodes("macro");
    for (Node node : macros) {
        Macro macro = new Macro();
        macro.setName(node.selectSingleNode("name").getText());
        macro.setText(node.selectSingleNode("text").getText());
        macro.setParent(site);
        site.add(macro);
    }

    @SuppressWarnings("unchecked")
    List<Node> nodes = root.selectNodes("template");
    for (Node node : nodes) {
        Template template = new Template();
        template.setType(TemplateType.getType(((Element) node).attributeValue("type")));
        template.setName(node.selectSingleNode("name").getText());
        template.setText(node.selectSingleNode("text").getText());
        template.setParent(site);
        if (template.isLayoutTemplate())
            site.setLayoutTemplate(template);
        else {
            site.add(template);
            templateCache.put(template.getName(), template);
        }
    }

    Element welcomePage = (Element) root.selectSingleNode("page");
    if (welcomePage != null) {
        importPage(site, welcomePage);
    } else {
        welcomePage = (Element) root.selectSingleNode("gallery");
        if (welcomePage != null)
            importGallery(site, welcomePage);
    }

    @SuppressWarnings("unchecked")
    List<Node> levels = root.selectNodes("level");
    for (Node node : levels) {
        importLevel(site, node);
    }
}

From source file:dk.dma.nogoservice.ExtractBagData.java

License:Apache License

@SneakyThrows(DocumentException.class)
private GridData readMetaData(String bagFile) {
    File xmlFile = new File(bagFile + ".xml");
    if (!xmlFile.exists()) {
        throw new ExitCodeException(NO_XML_FILE);
    }/* ww w .  ja v  a 2 s  . c o  m*/

    SAXReader reader = new SAXReader();
    Document document = reader.read(xmlFile);

    // we use fully qualified XPath, as we would like to fail if the format change
    Node coordinateBox = document.selectSingleNode(
            "/gmi:MI_Metadata/gmd:identificationInfo/bag:BAG_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:geographicElement/gmd:EX_GeographicBoundingBox");
    Node westLon = coordinateBox.selectSingleNode("gmd:westBoundLongitude/gco:Decimal");
    Node eastLon = coordinateBox.selectSingleNode("gmd:eastBoundLongitude/gco:Decimal");
    Node southLat = coordinateBox.selectSingleNode("gmd:southBoundLatitude/gco:Decimal");
    Node northLat = coordinateBox.selectSingleNode("gmd:northBoundLatitude/gco:Decimal");

    Node columnNode = document.selectSingleNode(
            "/gmi:MI_Metadata/gmd:spatialRepresentationInfo/gmd:MD_Georectified/gmd:axisDimensionProperties/gmd:MD_Dimension/gmd:dimensionName/gmd:MD_DimensionNameTypeCode[@codeListValue=\"column\"]/../../gmd:dimensionSize/gco:Integer");
    Node rowNode = document.selectSingleNode(
            "/gmi:MI_Metadata/gmd:spatialRepresentationInfo/gmd:MD_Georectified/gmd:axisDimensionProperties/gmd:MD_Dimension/gmd:dimensionName/gmd:MD_DimensionNameTypeCode[@codeListValue=\"row\"]/../../gmd:dimensionSize/gco:Integer");

    GridData gridData = new GridData();
    gridData.setLo1(Float.parseFloat(westLon.getText()));
    gridData.setLo2(Float.parseFloat(eastLon.getText()));
    gridData.setLa1(Float.parseFloat(southLat.getText()));
    gridData.setLa2(Float.parseFloat(northLat.getText()));
    gridData.setNx(Integer.parseInt(columnNode.getText()));
    gridData.setNy(Integer.parseInt(rowNode.getText()));

    return gridData;
}

From source file:edu.jhu.cvrg.ceptools.main.PubMedSearch.java

License:Apache License

public void processUrl() throws Exception {
    searcher = "title";
    initialProcess();//from w  w  w .j a  v  a  2  s . c om

    SAXReader reader = new SAXReader();
    SAXReader reader2 = new SAXReader();
    Document document;

    String mytitle, myabstract, myyear, myfullname;
    Element journalname, journalyear, journalmonth, journalday, journalvolume, journalissue, journalpagestart,
            epubday, epubmonth, epubyear, pubdoi;
    int mypmid;

    List<String> mylauthors = new ArrayList<String>();
    List<String> myfauthors = new ArrayList<String>();
    List<String> myfnames = new ArrayList<String>();

    //PubMed

    if (searcher.equalsIgnoreCase("title")) {

        int counter = 1;
        document = reader.read(url);
        @SuppressWarnings("rawtypes")
        List idlist = document.selectNodes("//IdList/Id");

        @SuppressWarnings("rawtypes")
        Iterator iditer = idlist.iterator();
        String pubmedlist = "";
        int x = 0;
        this.counter = 0;

        while (iditer.hasNext()) {
            Element pelement = (Element) iditer.next();
            if (x == 0) {
                pubmedlist += pelement.getText();
                x += 1;
            } else {
                pubmedlist += "," + pelement.getText();
            }
        }

        url = base + "efetch.fcgi?db=pubmed&id=" + pubmedlist + "&retmax=200&retmode=xml&rettype=abstract";
        Document pubdoc = reader2.read(url);

        @SuppressWarnings("unchecked")
        List<Node> thelist = pubdoc.selectNodes("//PubmedArticle| //PubmedBookArticle");

        Element abstractnode, titlenode, yearsnode, pmidnode;
        @SuppressWarnings("rawtypes")
        List firstnamenode;
        @SuppressWarnings("rawtypes")
        List lastnamenode;

        for (Node currnode : thelist) {
            mylauthors = new ArrayList<String>();
            myfauthors = new ArrayList<String>();
            myfnames = new ArrayList<String>();
            epubsum = epubsum2 = authorfull = "";

            titlenode = (Element) currnode.selectSingleNode(".//ArticleTitle | .//BookTitle");
            yearsnode = (Element) currnode
                    .selectSingleNode(".//PubDate/Year | .//DateCompleted/Year | .//DateCreated/Year");
            journalname = (Element) currnode.selectSingleNode(".//Journal/Title");
            journalyear = (Element) currnode.selectSingleNode(".//PubDate/Year");
            journalmonth = (Element) currnode.selectSingleNode(".//PubDate/Month");

            journalday = (Element) currnode.selectSingleNode(".//PubDate/Day");
            journalvolume = (Element) currnode.selectSingleNode(".//JournalIssue/Volume");
            journalissue = (Element) currnode.selectSingleNode(".//JournalIssue/Issue");
            journalpagestart = (Element) currnode.selectSingleNode(".//Pagination/MedlinePgn");

            epubday = (Element) document.selectSingleNode(
                    ".//PubMedPubDate[@PubStatus='aheadofprint']/Day  | .//PubMedPubDate[@PubStatus='epublish']/Day ");
            epubmonth = (Element) document.selectSingleNode(
                    ".//PubMedPubDate[@PubStatus='aheadofprint']/Month | .//PubMedPubDate[@PubStatus='epublish']/Month");
            epubyear = (Element) document.selectSingleNode(
                    ".//PubMedPubDate[@PubStatus='aheadofprint']/Year | .//PubMedPubDate[@PubStatus='epublish']/Year");

            pubdoi = (Element) currnode.selectSingleNode(".//ArticleId[@IdType='doi']");

            firstnamenode = currnode.selectNodes(".//ForeName");
            lastnamenode = currnode.selectNodes(".//LastName");
            abstractnode = (Element) currnode.selectSingleNode(".//Abstract/AbstractText[1]");
            pmidnode = (Element) currnode.selectSingleNode(".//PMID");

            myfnames = new ArrayList<String>();
            @SuppressWarnings("rawtypes")
            Iterator fiter = firstnamenode.iterator();
            @SuppressWarnings("rawtypes")
            Iterator liter = lastnamenode.iterator();

            if (journalname != null) {
                jn = journalname.getText();
            }
            if (journalvolume != null) {
                jv = journalvolume.getText();
            }
            if (journalissue != null) {
                ji = journalissue.getText();
            }
            if (journalmonth != null) {
                jm = journalmonth.getText();
            }
            if (journalyear != null) {
                jy = journalyear.getText();
            }
            if (journalpagestart != null) {
                jsp = journalpagestart.getText();
            }
            if (journalday != null) {
                jd = journalday.getText();
            }
            if (epubday != null) {
                epday = epubday.getText();
            }
            if (epubmonth != null) {
                epmonth = epubmonth.getText();
            }
            if (epubyear != null) {
                epyear = epubyear.getText();
            }
            if (pubdoi != null) {
                doi = "doi: " + pubdoi.getText();
            }
            if (jv.length() > 0) {
                epubsum2 += jv;
            }

            if (ji.length() > 0) {
                epubsum2 += "(" + ji + ")" + ":";
            }

            if (jsp.length() > 0) {
                epubsum2 += jsp + ".";
            }

            if (epmonth.length() < 1 && epyear.length() < 1 && epday.length() < 1) {
                epubsum = "[Epub ahead of print]";
            } else if (epyear.length() > 0) {
                epubsum = "Epub " + epyear + " " + epmonth + " " + epday;
            } else {
                epubsum = "";
            }

            mytitle = titlenode.getText();
            myyear = yearsnode.getText();
            mypmid = Integer.valueOf(pmidnode.getText());

            while (fiter.hasNext()) {
                Element fname = (Element) fiter.next();
                Element lname = (Element) liter.next();

                myfauthors.add(fname.getText());
                mylauthors.add(lname.getText());

                myfullname = fname.getText() + " " + lname.getText();
                myfnames.add(myfullname);

                if (fiter.hasNext()) {
                    authorfull = authorfull + myfullname + ", ";
                } else {
                    authorfull = authorfull + myfullname;
                }

            }

            if (abstractnode != null) {
                myabstract = abstractnode.getText();
            } else {
                myabstract = "NO ABSTRACT FOUND.";
            }

            publications.add(new Publication(mytitle, myabstract, myyear, myfauthors, mylauthors, myfnames, jv,
                    jn, jy, jm, jd, jsp, ji, epday, epmonth, epyear, doi, epubsum, epubsum2, authorfull, mypmid,
                    counter));

            counter++;

        }

    }

}