Example usage for org.w3c.dom Document getChildNodes

List of usage examples for org.w3c.dom Document getChildNodes

Introduction

In this page you can find the example usage for org.w3c.dom Document getChildNodes.

Prototype

public NodeList getChildNodes();

Source Link

Document

A NodeList that contains all children of this node.

Usage

From source file:plpwr.Task4.java

public Task4() throws Throwable {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    File file;//  w  ww  . j a  v a2  s. co  m
    try {
        File[] files = {};
        //            files = new Task2.FileDialogFluent(Window.getWindow(), "Choose file", FileDialog.LOAD).setVisibleFluent(true).getFiles();
        file = files[0];
    } catch (Throwable e) {
        file = new File("book.xml");
    }
    Document document = db.parse(file);
    //        List<Node> nodes = XmlUtil.asList(document.getChildNodes().item(0).getChildNodes());
    List<String> header = new ArrayList<>();
    Node root = null;
    for (int i = 0; i < document.getChildNodes().getLength(); ++i) {
        if (document.getChildNodes().item(i).getNodeName().equals("#text"))
            continue;
        root = document.getChildNodes().item(i);
    }
    NodeList nodes;
    nodes = root.getChildNodes();
    NodeList firstElement = null;
    for (int i = 0; i < nodes.getLength(); ++i) {
        if (nodes.item(i).getNodeName().equals("#text"))
            continue;
        firstElement = nodes.item(i).getChildNodes();
    }
    //        for (Node node : XmlUtil.asList(document.getChildNodes().item(0).getChildNodes().item(0).getChildNodes())) {
    //            header.add(node.getNodeName());
    //        }
    for (int i = 0; i < firstElement.getLength(); ++i) {
        //            System.out.println("ELO: "+firstElement.item(i).getNodeName());
        if (nodes.item(i).getNodeName().equals("#text"))
            continue;
        Element node = (Element) firstElement.item(i);
        header.add(node.getNodeName());
    }
    System.out.println("header: " + StringUtils.join(header.iterator(), '\t'));
    StringBuilder sb = new StringBuilder();
    sb.append(StringUtils.join(header.iterator(), ';'));
    //        for (Node node : nodes) {
    for (int i = 0; i < nodes.getLength(); i++) {
        //            System.out.println(nodes.item(i).getNodeName());
        if (nodes.item(i).getNodeName().equals("#text"))
            continue;
        Element node = (Element) nodes.item(i);
        NodeList inNodes = node.getChildNodes();
        //            for(Node n : XmlUtil.asList(e.getChildNodes())) {
        List<String> row = new ArrayList<>();
        for (int j = 0; j < inNodes.getLength(); j++) {
            //                System.out.println(inNodes.item(j).getNodeName());
            if (inNodes.item(j).getNodeType() != Node.ELEMENT_NODE)
                continue;
            String content = (inNodes.item(j).getChildNodes().item(0)).getNodeValue();
            content = content.replaceAll("\\s+", " ");
            row.add(content);
        }
        System.out.println("\nrow:\t" + StringUtils.join(row.iterator(), '\t'));
        sb.append(StringUtils.join(row.iterator(), '\t'));
    }
    setAns(sb.toString());

}

From source file:psidev.psi.mi.filemakers.xsd.AbstractXsdTreeStruct.java

public void loadSchema(URL schemaUrl) throws FileNotFoundException, IOException {
    emptySelectionLists();// w  w  w  .  j av  a2 s.  com

    this.schemaURL = schemaUrl;

    InputStream in = schemaURL.openStream();

    /* test: get keyz/keyref */
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder parser = factory.newDocumentBuilder();
        Document d = parser.parse(in);
        for (int i = 0; i < d.getChildNodes().getLength(); i++) {
            getKeys(d.getChildNodes().item(i));
        }

    } catch (ParserConfigurationException e) {
        JOptionPane.showMessageDialog(new JFrame(), "Cannot load the schema.", "ERROR",
                JOptionPane.ERROR_MESSAGE);
        log.error(e);
    } catch (SAXException e) {
        JOptionPane.showMessageDialog(new JFrame(), "Cannot load the schema.", "ERROR",
                JOptionPane.ERROR_MESSAGE);
        log.error(e);
    } catch (IOException e) {
        JOptionPane.showMessageDialog(new JFrame(), "Cannot load the schema.", "ERROR",
                JOptionPane.ERROR_MESSAGE);
        log.error(e);
    }

    in = schemaURL.openStream();
    SchemaReader reader = new SchemaReader(new InputSource(in));
    schema = reader.read();
    createTree();
    Utils.lastVisitedDirectory = schemaURL.getPath();
    Utils.lastVisitedSchemaDirectory = schemaURL.getPath();
}

From source file:ru.codeinside.adm.ui.UploadDeployer.java

private boolean hasApiServer(String supportedInterface, JarInputStream jarStream)
        throws IOException, SAXException, ParserConfigurationException {
    for (String xmlPath : jarStream.getManifest().getMainAttributes().getValue("Service-Component")
            .split(",")) {
        ZipEntry nextZipEntry = jarStream.getNextEntry();
        while (nextZipEntry != null && !xmlPath.equals(nextZipEntry.getName())) {
            nextZipEntry = jarStream.getNextEntry();
        }/* www  . j a  v  a2  s  .co m*/
        Document xmlDoc = readXml(jarStream);

        Node node = getChildForNlByNames(xmlDoc.getChildNodes(), "component", "service", "provide");
        if (node != null) {
            String interfaceValue = node.getAttributes().getNamedItem("interface").getNodeValue();

            if (StringUtils.isNotEmpty(interfaceValue) && supportedInterface.equals(interfaceValue.trim())) {
                return true;
            }
        }
    }
    return false;
}

From source file:slash.navigation.converter.gui.services.CrossingWays.java

String extractResult(String result) throws IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    DocumentBuilder builder;/*from   w  ww.  j a v  a 2 s.c o m*/
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new IOException(e);
    }

    Document document;
    try {
        document = builder.parse(new InputSource(new StringReader(result)));
    } catch (SAXException e) {
        throw new IOException(e);
    }

    NodeList nodeList = document.getChildNodes();
    if (nodeList.getLength() > 0) {
        Node node = nodeList.item(0);
        return node.getTextContent();
    }
    return null;
}

From source file:ubic.gemma.core.image.aba.AllenBrainAtlasServiceImpl.java

private Collection<ImageSeries> extractSeries(Document imageSeriesDoc) {
    NodeList sectionDataSets = imageSeriesDoc.getChildNodes().item(0).getChildNodes().item(0).getChildNodes();
    Collection<ImageSeries> series = new LinkedList<>();

    for (int i = 0; i < sectionDataSets.getLength(); i++) {
        Node node = sectionDataSets.item(i);
        if (node.getNodeName().equals("section-data-set")) {
            String idText = XMLUtils.extractOneChildText(node, "id");
            if (idText == null) {
                continue;
            }//from w ww  .j  a  v  a  2  s .  c o  m

            ImageSeries is = new ImageSeries(Integer.parseInt(idText));

            Node images = XMLUtils.extractOneChild(node, "section-images");

            if (images == null) {
                AllenBrainAtlasServiceImpl.log.warn("ABA series " + idText + " did not contain any images.");
                continue;
            }

            is.setImages(this.getImageSeriesImages(images.getChildNodes()));
            series.add(is);
        }
    }
    return series;
}

From source file:ubic.gemma.image.aba.AllenBrainAtlasServiceImpl.java

/**
 * Given a gene too look for for will return the coressponding abaGene (useful for finding images)
 * //from  w  ww  .  ja v a2 s . c  o  m
 * @param givenGene symbol of gene that will be used to search ABA.
 * @return
 */
protected AbaGene getGene(String givenGene, boolean correctCase) throws IOException {
    String gene = givenGene;

    if (correctCase) {
        gene = correctCase(gene);
    }

    File outputFile = getFile(gene);
    Document geneDoc = null;

    try {
        FileOutputStream out = new FileOutputStream(outputFile);
        this.getGene(gene, out);
        out.close();

        geneDoc = XMLUtils.openAndParse(new FileInputStream(outputFile));
    } catch (ParserConfigurationException pce) {
        log.warn(pce);
        return null;
    } catch (SAXException se) {
        log.warn(se);
    } catch (FileNotFoundException fnfe) {
        return null;
    }

    if (geneDoc == null) {
        return null;
    }

    Collection<String> xmlData = XMLUtils.extractTagData(geneDoc, "geneid");
    Integer geneId = xmlData.isEmpty() ? null : Integer.parseInt(xmlData.iterator().next());

    xmlData = XMLUtils.extractTagData(geneDoc, "genename");
    String geneName = xmlData.isEmpty() ? null : xmlData.iterator().next();

    xmlData = XMLUtils.extractTagData(geneDoc, "genesymbol");
    String geneSymbol = xmlData.isEmpty() ? null : xmlData.iterator().next();

    xmlData = XMLUtils.extractTagData(geneDoc, "entrezgeneid");
    Integer entrezGeneId = xmlData.isEmpty() ? null : Integer.parseInt(xmlData.iterator().next());

    xmlData = XMLUtils.extractTagData(geneDoc, "ncbiaccessionnumber");
    String ncbiAccessionNumber = xmlData.isEmpty() ? null : xmlData.iterator().next();

    String geneUrl = (geneSymbol == null) ? null : this.getGeneUrl(geneSymbol);

    if (geneId == null && geneSymbol == null)
        return null;

    AbaGene geneData = new AbaGene(geneId, geneSymbol, geneName, entrezGeneId, ncbiAccessionNumber, geneUrl,
            null);

    NodeList idList = geneDoc.getChildNodes().item(0).getChildNodes();

    // log.debug( "Got " + idList.getLength() );

    for (int i = 0; i < idList.getLength(); i++) {
        Node item = idList.item(i);

        if (!item.getNodeName().equals("image-series"))
            continue;

        NodeList imageSeriesList = item.getChildNodes();

        for (int j = 0; j < imageSeriesList.getLength(); j++) {

            Node imageSeries = imageSeriesList.item(j);

            NodeList childNodes = imageSeries.getChildNodes();
            Integer imageSeriesId = null;
            String plane = null;

            for (int m = 0; m < childNodes.getLength(); m++) {

                Node c = childNodes.item(m);

                // log.info( c.getNodeName() );
                String n = c.getNodeName();
                try {
                    if (n.equals("imageseriesid")) {
                        imageSeriesId = Integer.parseInt(XMLUtils.getTextValue((Element) c));
                    } else if (n.equals("plane")) {
                        plane = XMLUtils.getTextValue((Element) c);
                    } else {
                        // Just skip and check the next one.
                    }
                } catch (IOException ioe) {
                    log.warn(ioe);
                }

            }

            if (imageSeriesId != null && plane != null) {
                ImageSeries is = new ImageSeries(imageSeriesId, plane);
                geneData.addImageSeries(is);
                log.debug("added image series to gene data");
            } else {
                log.debug("Skipping adding imageSeries to gene cause data missing");
            }

        }
    }

    return geneData;

}

From source file:ubic.gemma.image.aba.AllenBrainAtlasServiceImpl.java

@Override
public Collection<Image> getImageseries(Integer imageseriesId) {

    File outputFile = getFile("ImageseriesId_" + imageseriesId.toString());
    Document imageSeriesDoc = null;

    try {/*  w  w  w.  ja v a2 s .c  om*/
        FileOutputStream out = new FileOutputStream(outputFile);
        this.getImageseries(imageseriesId, out);
        imageSeriesDoc = XMLUtils.openAndParse(new FileInputStream(outputFile));
    } catch (Exception e) {
        log.error(e.getMessage(), e.getCause());
        return null;
    }

    NodeList idList = imageSeriesDoc.getChildNodes().item(0).getChildNodes();
    Collection<Image> results = new HashSet<Image>();

    for (int i = 0; i < idList.getLength(); i++) {
        Node item = idList.item(i);

        if (!item.getNodeName().equals("images"))
            continue;

        NodeList imageList = item.getChildNodes();

        for (int j = 0; j < imageList.getLength(); j++) {

            Node image = imageList.item(j);

            if (!image.getNodeName().equals("image"))
                continue;

            NodeList childNodes = image.getChildNodes();

            Integer imageId = null;
            String displayName = null;
            Integer position = null;
            Integer referenceAtlasIndex = null;
            String thumbnailUrl = null;
            String zoomifiedNisslUrl = null;
            String expressionThumbnailUrl = null;
            String downloadImagePath = null;
            String downloadExpressionPath = null;

            for (int m = 0; m < childNodes.getLength(); m++) {

                Node c = childNodes.item(m);

                // log.info( c.getNodeName() );
                String n = c.getNodeName();
                try {
                    if (n.equals("#text")) {
                        continue; // added to make faster as half of comparisions are empty nodes of this type!
                    } else if (n.equals("imageid")) {
                        imageId = Integer.parseInt(XMLUtils.getTextValue((Element) c));
                    } else if (n.equals("imagedisplayname")) {
                        displayName = XMLUtils.getTextValue((Element) c);
                    } else if (n.equals("position")) {
                        position = Integer.parseInt(XMLUtils.getTextValue((Element) c));
                    } else if (n.equals("referenceatlasindex")) {
                        referenceAtlasIndex = Integer.parseInt(XMLUtils.getTextValue((Element) c));
                    } else if (n.equals("thumbnailurl")) {
                        thumbnailUrl = XMLUtils.getTextValue((Element) c);
                    } else if (n.equals("zoomifiednisslurl")) {
                        zoomifiedNisslUrl = XMLUtils.getTextValue((Element) c);
                    } else if (n.equals("expressthumbnailurl")) {
                        expressionThumbnailUrl = XMLUtils.getTextValue((Element) c);
                    } else if (n.equals("downloadImagePath")) {
                        downloadImagePath = XMLUtils.getTextValue((Element) c);
                    } else if (n.equals("downloadExpressionPath")) {
                        downloadExpressionPath = XMLUtils.getTextValue((Element) c);
                    } else {
                        continue;
                    }
                } catch (IOException ioe) {
                    log.warn(ioe);
                }
            } // for loop

            if (imageId != null && downloadImagePath != null) {
                Image img = new Image(displayName, imageId, position, referenceAtlasIndex, thumbnailUrl,
                        zoomifiedNisslUrl, expressionThumbnailUrl, downloadImagePath, downloadExpressionPath, 0,
                        0);
                results.add(img);
            } else {
                log.info(
                        "Skipping adding image to collection cause necessary data missing after parsing image xml");
            }

        }
    }

    return results;

}