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:com.rover12421.shaka.apktool.lib.AndrolibResourcesAj.java

private boolean horizontalScrollView_check(String errInfo) throws Exception {
    if (errInfo.indexOf("'@android:style/Widget.HorizontalScrollView'") > 0) {
        Pattern xmlPathPattern = Pattern.compile(
                "(.+?):\\d+:.+?(Error retrieving parent for item).+?'@android:style/Widget.HorizontalScrollView'");
        Matcher xmlPathMatcher = xmlPathPattern.matcher(errInfo);
        if (xmlPathMatcher.find()) {
            String xmlPathStr = xmlPathMatcher.group(1);
            File xmlPathFile = new File(xmlPathStr);
            if (xmlPathFile.exists()) {
                LogHelper.warning("Find HorizontalScrollView exception xml : " + xmlPathStr);
                DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
                Document document = documentBuilder.parse(xmlPathStr);
                NodeList list = document.getChildNodes().item(0).getChildNodes();
                for (int i = 0; i < list.getLength(); i++) {
                    Node node = list.item(i);
                    if (node.getAttributes() != null) {
                        Node attr = node.getAttributes().getNamedItem("parent");
                        if (attr != null
                                && attr.getNodeValue().equals("@android:style/Widget.HorizontalScrollView")) {
                            /**
                             * HorizontalScrollView??aaptScrollView
                             *//*from   w ww  .j a va2  s . c o m*/
                            attr.setNodeValue("@android:style/Widget.ScrollView");
                            /**
                             * ? android:scrollbars  android:fadingEdge 
                             * ??,
                             * ?????
                             */
                            Element scrollbars = document.createElement("item");
                            scrollbars.setAttribute("name", "android:scrollbars");
                            scrollbars.setNodeValue("horizontal");

                            Element fadingEdge = document.createElement("item");
                            fadingEdge.setAttribute("name", "android:fadingEdge");
                            fadingEdge.setNodeValue("horizontal");
                            Element element = (Element) node;
                            NodeList items = element.getElementsByTagName("item");
                            for (int j = 0; j < items.getLength(); j++) {
                                Element item = (Element) items.item(j);
                                if (item.getAttribute("name").equals("android:scrollbars")) {
                                    scrollbars = null;
                                }
                                if (item.getAttribute("name").equals("android:fadingEdge")) {
                                    fadingEdge = null;
                                }
                            }

                            if (scrollbars != null) {
                                node.appendChild(scrollbars);
                            }

                            if (fadingEdge != null) {
                                node.appendChild(fadingEdge);
                            }
                        }
                    }
                }

                /**
                 * ?xml
                 */
                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                Transformer transformer = transformerFactory.newTransformer();
                transformer.transform(new DOMSource(document), new StreamResult(xmlPathStr));

                return true;
            }
        }
    }

    return false;
}

From source file:com.esri.gpt.server.openls.provider.services.reversegeocode.ReverseGeocodeProvider.java

/**
 * Parses reverse Geocode response./*  w ww.ja va  2s.c  o m*/
 * @param sResponse
 * @return
 * @throws Throwable
 */
private GeocodedAddress parseReverseGeocodeResponse(String sResponse) throws Throwable {
    GeocodedAddress addr = new GeocodedAddress();
    try {

        JSONObject jResponse = new JSONObject(sResponse);

        String xResponse = "<?xml version='1.0'?><response>" + org.json.XML.toString(jResponse) + "</response>";
        LOGGER.info("XML from JSON = " + xResponse);

        String addressName = "";

        Address respAddr = null;
        String street = "";
        String intStreet = "";
        String city = "";
        String state = "";
        String zip = "";
        String scoreStr = "";
        String x = "";
        String y = "";
        String country = "US";

        DocumentBuilderFactory xfactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = xfactory.newDocumentBuilder();
        InputSource inStream = new InputSource();
        inStream.setCharacterStream(new StringReader(xResponse));
        Document doc = db.parse(inStream);

        doc.getDocumentElement().normalize();
        LOGGER.info("Root element " + doc.getDocumentElement().getNodeName());
        NodeList nodeLst = doc.getChildNodes();
        LOGGER.info("Information of all candidates:");

        for (int s = 0; s < nodeLst.getLength(); s++) {
            Node fstNode = nodeLst.item(s);
            Element fstElmnt = (Element) fstNode;

            street = "";
            intStreet = "";
            city = "";
            state = "";
            zip = "";
            scoreStr = "";

            // LOCATION
            NodeList locationList = fstElmnt.getElementsByTagName("location");
            Element fstNmElmnt = (Element) locationList.item(0);
            NodeList nodeY = fstNmElmnt.getElementsByTagName("y");
            y = nodeY.item(0).getTextContent();
            LOGGER.info("y = " + y);
            NodeList nodeX = fstNmElmnt.getElementsByTagName("x");
            x = nodeX.item(0).getTextContent();
            LOGGER.info("x = " + x);

            // ADDRESS
            NodeList addressList = fstElmnt.getElementsByTagName("address");
            Node addressNode = addressList.item(0);
            addressName = addressList.item(0).getTextContent();
            LOGGER.info("addressName = " + addressName);
            NodeList children = addressNode.getChildNodes();
            for (int i = 0; i < children.getLength(); i++) {
                Node child = children.item(i);
                if (child.getNodeName().equalsIgnoreCase("address")) {
                    street = child.getTextContent();
                } else if (child.getNodeName().equalsIgnoreCase("city")) {
                    city = child.getTextContent();
                } else if (child.getNodeName().equalsIgnoreCase("zip")) {
                    zip = child.getTextContent();
                } else if (child.getNodeName().equalsIgnoreCase("state")) {
                    state = child.getTextContent();
                } else if (child.getNodeName().equalsIgnoreCase("country")) {
                    country = child.getTextContent();
                }
            }

            // SCORE
            NodeList scoreList = fstElmnt.getElementsByTagName("score");
            if (scoreList != null && scoreList.getLength() > 0) {
                scoreStr = scoreList.item(0).getTextContent();
                new Double(scoreStr);
                LOGGER.info("score = " + scoreStr);
            }

            // NOW ADD THIS RESULT TO THE OUTPUT pos
            respAddr = new Address();
            respAddr.setStreet(street);
            respAddr.setMunicipality(city);
            respAddr.setPostalCode(zip);
            respAddr.setCountrySubdivision(state);
            respAddr.setIntersection(intStreet);

            addr.setX(x);
            addr.setY(y);
            addr.setAddress(respAddr);
            addr.setCountry(country);

        }
    } catch (Exception p_e) {
        LOGGER.severe("Caught Exception" + p_e.getMessage());
    }
    return addr;
}

From source file:de.haber.xmind2latex.XMindToLatexExporter.java

public void convert() throws ParserConfigurationException, SAXException, IOException {
    InputStream is = getxMindSourceAsStream();
    if (is == null) {
        throw new ParserConfigurationException("Call configure() before convert().");
    }/* w  w w  .j  a v  a2  s .  c  o  m*/
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(is);
    is.close();
    StringBuilder sb = new StringBuilder();

    sb.append(convert(document.getChildNodes()));

    String text = sb.toString();
    save(text);
}

From source file:io.sunrisedata.wikipedia.WikipediaPageRevision.java

public void readFromXml(String xml) throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new StringInputStream(xml));

    // and now the fun part

    NodeList n = doc.getChildNodes().item(0).getChildNodes();
    for (int i = 0; i < n.getLength(); i++) {
        Node node = n.item(i);/*from   w  ww.  ja va  2 s .c  o m*/
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element e = (Element) node;
            switch (e.getTagName()) {
            case XML_TAG_CONTRIBUTOR:
                NodeList contribNodes = e.getChildNodes();
                for (int j = 0; j < contribNodes.getLength(); j++) {
                    Node contribNode = contribNodes.item(j);
                    if (contribNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element contribEl = (Element) contribNode;
                        switch (contribEl.getTagName()) {
                        case XML_TAG_CONTRIBUTOR_ID:
                            this.contributorId = contribEl.getTextContent();
                            break;
                        case XML_TAG_CONTRIBUTOR_IP:
                            this.contributorIp = contribEl.getTextContent();
                            break;
                        case XML_TAG_CONTRIBUTOR_USERNAME:
                            this.contributorUsername = contribEl.getTextContent();
                            break;
                        }
                    }
                }
                break;

            case XML_TAG_TEXT:
                contentWikiMarkup = e.getTextContent();
                if (e.hasAttribute(XML_ATTRIBUTE_TEXT_BYTES)) {
                    this.declaredContentLength = Integer.parseInt(e.getAttribute(XML_ATTRIBUTE_TEXT_BYTES));
                    if (this.declaredContentLength > 0 && isEmpty()) {
                        this.isMetadata = true;
                    }
                }
                // determine if article is a disambiguation, redirection, and/or stub page.
                // the first characters of the text must be equal to IDENTIFIER_REDIRECTION_UPPERCASE or IDENTIFIER_REDIRECTION_LOWERCASE
                this.isRedirect = contentWikiMarkup.startsWith(IDENTIFIER_REDIRECTION_LOWERCASE)
                        || contentWikiMarkup.startsWith(IDENTIFIER_REDIRECTION_UPPERCASE);

                // to be a stub, the article must contain the IDENTIFIER_STUB_WIKIPEDIA_NAMESPACE or IDENTIFIER_STUB_TEMPLATE
                this.isStub = contentWikiMarkup.contains(IDENTIFIER_STUB_TEMPLATE);

                break;

            case XML_TAG_ID:
                this.revisionId = e.getTextContent();
                break;

            case XML_TAG_TIMESTAMP:
                this.timestamp = e.getTextContent();
                break;

            case XML_TAG_MINOR:
                // presence of the empty <minor/> tag indicates it is a minor revision
                this.isMinor = true;
                break;

            case XML_TAG_COMMENT:
                this.comment = e.getTextContent();
                break;

            case XML_TAG_SHA1:
                this.sha1 = e.getTextContent();
                break;

            case XML_TAG_MODEL:
                this.model = e.getTextContent();
                break;

            case XML_TAG_FORMAT:
                this.format = e.getTextContent();
                break;

            case XML_TAG_PARENTID:
                this.parentRevisionId = e.getTextContent();
                break;
            }
        }
    }

}

From source file:com.blackbear.flatworm.ConfigurationReader.java

public FileFormat loadConfigurationFile(InputStream in)
        throws FlatwormUnsetFieldValueException, FlatwormConfigurationValueException {
    DocumentBuilder parser;/*from  w ww .  ja v a2  s  . co  m*/
    Document document;
    NodeList children;

    try {
        DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
        parser = fact.newDocumentBuilder();
        document = parser.parse((new org.xml.sax.InputSource(in)));
        children = document.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (("file-format".equals(child.getNodeName())) && (child.getNodeType() == Node.ELEMENT_NODE)) {
                return (FileFormat) traverse(child);
            }
        }
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.zoho.creator.jframework.XMLParser.java

static List<ZCChoice> parseLookUpChoices(Document rootDocument) {

    NodeList nl = rootDocument.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node responseNode = nl.item(i);
        if (responseNode.getNodeName().equals("response")) {
            NodeList responseNodeList = responseNode.getChildNodes();
            for (int j = 0; j < responseNodeList.getLength(); j++) {
                Node resultNode = responseNodeList.item(j);
                if (resultNode.getNodeName().equals("result")) {
                    NodeList resultNodeList = resultNode.getChildNodes();
                    for (int k = 0; k < resultNodeList.getLength(); k++) {
                        Node fieldNode = resultNodeList.item(k);
                        if (fieldNode.getNodeName().equals("field")) {
                            NodeList fieldNodeList = fieldNode.getChildNodes();
                            for (int l = 0; l < fieldNodeList.getLength(); l++) {
                                Node choicesNode = fieldNodeList.item(l);
                                if (choicesNode.getNodeName().equals("choices")) {
                                    return parseLookUpChoices(choicesNode);
                                }//w  w w  . j a v  a 2  s  .  c o m
                            }
                        }
                    }
                }
            }
        }

    }
    return new ArrayList<ZCChoice>();
}

From source file:com.zoho.creator.jframework.XMLParser.java

static void parseAndAddRecords(Document rootDocument, ZCView zcView) {
    NodeList nl = rootDocument.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node responseNode = nl.item(i);
        if (responseNode.getNodeName().equals("response")) {
            NodeList responseNodes = responseNode.getChildNodes();
            for (int j = 0; j < responseNodes.getLength(); j++) {
                Node responseChildNode = responseNodes.item(j);
                if (responseChildNode.getNodeName().equals("records")) {
                    parseAndSetRecords(zcView, responseChildNode);
                } else if (responseChildNode.getNodeName().equals("calendar")) {
                    parseAndSetCalendarRecords(zcView, responseChildNode);
                }/*w  ww  .j  ava2  s  . c  o m*/
            }
        }
    }
}

From source file:com.enonic.esl.xml.XMLTool.java

public static Element createRootElement(Document doc, String name) {

    if (name == null) {
        throw new XMLToolException("Root element name cannot be null!");
    } else if (name.trim().length() == 0) {
        throw new XMLToolException("Root element name has to contain at least one character!");
    }//  w  ww .  j  a  v a 2 s .  co  m

    // remove old root
    NodeList nodes = doc.getChildNodes();
    Node[] element = filterNodes(nodes, Node.ELEMENT_NODE);
    for (int i = 0; i < element.length; i++) {
        doc.removeChild(element[i]);
    }

    // create and append the new root
    Element root = doc.createElement(name);
    doc.appendChild(root);

    return root;
}

From source file:com.zoho.creator.jframework.XMLParser.java

static String parsePivotViewURL(Document document) {
    String pivotViewUrl = "";
    NodeList nl = document.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node responseNode = nl.item(i);
        if (responseNode.getNodeName().equals("response")) {

            NodeList responseNodes = responseNode.getChildNodes();
            for (int j = 0; j < responseNodes.getLength(); j++) {
                Node resultNode = responseNodes.item(j);

                if (resultNode.getNodeName().equals("result")) {

                    NodeList resultNodes = resultNode.getChildNodes();

                    for (int k = 0; k < resultNodes.getLength(); k++) {

                        Node reportsUrl = resultNodes.item(k);

                        if (reportsUrl.getNodeName().equals("reportsUrl")) {

                            pivotViewUrl = getStringValue(reportsUrl, pivotViewUrl);
                        }//from  w  w  w .  j  a  va  2 s .c o  m
                    }
                }
            }
        }
    }

    return pivotViewUrl;
}

From source file:cz.tyr.android.currencyrates.bank.RaiffeisenBank.java

@Override
public int downloadData() {
    if (mCurrency == null) {
        mCurrency = getDefaultCurrencyValue();
    }// www. j av a 2  s  .  c o m

    String dateStr = null;
    String rateStr = null;

    String url = getDataUrl();

    if (DEBUG > 0) {
        Log.d(TAG, "Download data for RB_CZ");
        Log.d(TAG, " * url = : " + url);
        Log.d(TAG, " * currency = " + mCurrency);
        Log.d(TAG, " * exchange = " + mExchangeType);
        Log.d(TAG, " * direction = " + mExchangeDirection);
    }

    HttpClient sClient = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);
    InputStream stream = null;

    try {
        stream = sClient.execute(request).getEntity().getContent();
    } catch (IOException e) {
        Log.d(TAG, "Problem downloading the XML data.");
        return 1;
    }

    try {
        if (DEBUG > 1)
            Log.d(TAG, " - Factory start");
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        Document doc = dbf.newDocumentBuilder().parse(stream);
        if (DEBUG > 1)
            Log.d(TAG, " - Factory end");

        if (doc != null && doc.hasChildNodes()) {
            if (DEBUG > 1)
                Log.d(TAG, " - Parse start");

            // find the root element
            for (int i = 0; i < doc.getChildNodes().getLength(); i++) {
                Node root = doc.getChildNodes().item(i);

                if (root.getNodeType() == Node.ELEMENT_NODE || root.getNodeName().equals("exchange_rates")) {
                    NodeList list = doc.getChildNodes().item(i).getChildNodes();

                    // find first node
                    for (int j = 0; j < list.getLength(); j++) {
                        Node n = list.item(j);

                        // check the attributes of this element
                        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals("exchange_rate")) {
                            boolean go = false;

                            if (DEBUG > 1)
                                Log.d(TAG, " # Got EXCHANGE_RATE element!");

                            for (int k = 0; k < n.getAttributes().getLength(); k++) {
                                Node a = n.getAttributes().item(k);

                                if (a.getNodeName().equals("type") && a.getNodeValue().equals(
                                        "XML_RATE_TYPE_EBNK_" + mExchangeDirection + "_" + mExchangeType)) {
                                    if (DEBUG > 1)
                                        Log.d(TAG, " - CORRECT ELEMENT! TAKE THE DATE!");

                                    go = true;
                                } else if (go && a.getNodeName().equals("valid_from")) {
                                    if (DEBUG > 1)
                                        Log.d(TAG, " - GOT DATE! " + a.getNodeValue());

                                    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
                                    Date fdate = (Date) formatter.parse(a.getNodeValue());
                                    formatter = new SimpleDateFormat(
                                            mResources.getString(R.string.date_time_format));

                                    dateStr = formatter.format(fdate);

                                    // stop the loop
                                    break;
                                }
                            }

                            // if it is correct element, go for the rate
                            if (go) {
                                if (DEBUG > 1)
                                    Log.d(TAG, " - Searching for the rate!");

                                NodeList currencies = n.getChildNodes();

                                // check the attributes
                                for (int k = 0; k < currencies.getLength(); k++) {
                                    Node c = currencies.item(k);

                                    if (c.getNodeType() == Node.ELEMENT_NODE
                                            && c.getNodeName().equals("currency")) {
                                        boolean bool = false;
                                        String rateTmp = null;

                                        for (int l = 0; l < c.getAttributes().getLength(); l++) {
                                            Node a = c.getAttributes().item(l);

                                            if (a.getNodeName().equals("name")
                                                    && a.getNodeValue().equals(mCurrency)) {
                                                if (DEBUG > 1)
                                                    Log.d(TAG, " -- Got the Currency!!!");

                                                bool = true;
                                            } else if (a.getNodeName().equals("rate")) {
                                                if (DEBUG > 1)
                                                    Log.d(TAG, " -- Got the RATE!!!" + a.getNodeValue());
                                                rateTmp = a.getNodeValue();
                                            }

                                            if (bool && rateTmp != null) {
                                                rateStr = rateTmp;

                                                if (DEBUG > 1)
                                                    Log.d(TAG, " -- Got the Currency VALUE: " + rateStr);

                                                // stop the loop
                                                break;
                                            }
                                        }
                                    }

                                    // stop the loop
                                    if (rateStr != null) {
                                        break;
                                    }
                                }
                            }
                        }

                        // stop the loop
                        if (rateStr != null) {
                            break;
                        }
                    }
                }

                // stop the loop
                if (rateStr != null) {
                    break;
                }
            }

            if (DEBUG > 1)
                Log.d(TAG, " - Parse end");
        }
    } catch (Exception e) {
        e.printStackTrace();
        return 1;
    }

    // Check the values
    if (dateStr == null || rateStr == null) {
        Log.d(TAG, " # One of the values is null!");
        return 1;
    }

    setCurrencyDate(dateStr);
    setCurrencyRate(Float.parseFloat(rateStr));

    return 0;
}