Example usage for org.w3c.dom Element getTextContent

List of usage examples for org.w3c.dom Element getTextContent

Introduction

In this page you can find the example usage for org.w3c.dom Element getTextContent.

Prototype

public String getTextContent() throws DOMException;

Source Link

Document

This attribute returns the text content of this node and its descendants.

Usage

From source file:com.t2tierp.controller.nfe.EnviaNfe.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public Map consultaEnvioNfe(String numeroRecibo, String xmlEnviNfe, String codigoUf, String ambiente)
        throws Exception {
    Map map = new HashMap();

    String url = "";
    if (codigoUf.equals("52")) {
        if (ambiente.equals("1")) {
            url = "https://nfe.sefaz.go.gov.br/nfe/services/v2/NfeRetAutorizacao?wsdl";
        } else if (ambiente.equals("2")) {
            url = "https://homolog.sefaz.go.gov.br/nfe/services/v2/NfeRetAutorizacao?wsdl";
        }/*from  w  w  w.  j  a  va 2 s .co m*/
    }
    /* fica a cargo de cada participante definir a url que sera utiizada de acordo com o codigo da UF
     * URLs disponiveis em:
     * Homologacao: http://hom.nfe.fazenda.gov.br/PORTAL/WebServices.aspx
     * Producao: http://www.nfe.fazenda.gov.br/portal/WebServices.aspx
     */

    if (url.equals("")) {
        throw new Exception("URL de retorno da sefaz no definida para o cdigo de UF = " + codigoUf);
    }

    String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
            + "<consReciNFe versao=\"3.10\" xmlns=\"http://www.portalfiscal.inf.br/nfe\">" + "<tpAmb>"
            + ambiente + "</tpAmb>" + "<nRec>" + numeroRecibo + "</nRec>" + "</consReciNFe>";

    OMElement omeElement = AXIOMUtil.stringToOM(xml);

    NfeRetAutorizacaoStub.NfeDadosMsg dadosMsg = new NfeRetAutorizacaoStub.NfeDadosMsg();
    dadosMsg.setExtraElement(omeElement);

    NfeRetAutorizacaoStub.NfeCabecMsg cabecMsg = new NfeRetAutorizacaoStub.NfeCabecMsg();
    cabecMsg.setCUF(codigoUf);
    cabecMsg.setVersaoDados("3.10");

    NfeRetAutorizacaoStub.NfeCabecMsgE cabecMsgE = new NfeRetAutorizacaoStub.NfeCabecMsgE();
    cabecMsgE.setNfeCabecMsg(cabecMsg);

    NfeRetAutorizacaoStub stub = new NfeRetAutorizacaoStub(url);

    NfeRetAutorizacaoStub.NfeRetAutorizacaoLoteResult result = stub.nfeRetAutorizacaoLote(dadosMsg, cabecMsgE);

    //System.out.println(result.getExtraElement().toString());
    ByteArrayInputStream in = new ByteArrayInputStream(result.getExtraElement().toString().getBytes("UTF-8"));
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    Document docResposta = dbf.newDocumentBuilder().parse(in);

    NodeList nodeListMotivo = docResposta.getDocumentElement().getElementsByTagName("xMotivo");
    NodeList nodeListProt = docResposta.getDocumentElement().getElementsByTagName("protNFe");
    String respostaSefaz = "";
    String xmlProt = "";
    String xmlProc = "";
    String xmlNfe = "";
    boolean autorizado = false;

    for (int i = 0; i < nodeListMotivo.getLength(); i++) {
        Element element = (Element) nodeListMotivo.item(i);
        respostaSefaz += element.getTextContent() + "\n";
        if (element.getTextContent().startsWith("Autorizado")) {
            autorizado = true;
        }
    }

    map.put("autorizado", autorizado);
    map.put("resposta", respostaSefaz);
    map.put("numeroRecibo", numeroRecibo);
    map.put("xmlEnviNfe", xmlEnviNfe);

    if (autorizado) {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer();

        ByteArrayOutputStream outProt = new ByteArrayOutputStream();
        trans.transform(new DOMSource(nodeListProt.item(0)), new StreamResult(outProt));
        xmlProt = outProt.toString().replaceAll("<\\?xml version=\"1.0\" encoding=\"UTF-8\"\\?>", "");

        ByteArrayInputStream inEnviNfe = new ByteArrayInputStream(xmlEnviNfe.getBytes());
        Document docEnviNfe = dbf.newDocumentBuilder().parse(inEnviNfe);
        ByteArrayOutputStream outNfe = new ByteArrayOutputStream();
        trans.transform(new DOMSource(docEnviNfe.getElementsByTagName("NFe").item(0)),
                new StreamResult(outNfe));
        xmlNfe = outNfe.toString().replaceAll("<\\?xml version=\"1.0\" encoding=\"UTF-8\"\\?>", "");

        xmlProc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
        xmlProc += "<nfeProc versao=\"3.10\" xmlns=\"http://www.portalfiscal.inf.br/nfe\">";
        xmlProc += xmlNfe;
        xmlProc += xmlProt;
        xmlProc += "</nfeProc>";

        map.put("xmlProc", xmlProc);
    }

    return map;
}

From source file:org.pac4j.saml.client.Saml2ClientWrapper.java

@Override
protected Saml2Profile retrieveUserProfile(final Saml2WrapperCredentials credentials,
        final WebContext context) {

    String overwriteId = null;/*from   w w  w.  j av a  2  s . c  o m*/

    Saml2Profile profile = new Saml2Profile();

    profile.setId(credentials.getNameId().getValue());

    for (Attribute attribute2 : credentials.getAttributes()) {

        AttributeImpl attribute = (AttributeImpl) attribute2;

        List<String> values = new ArrayList<String>();

        for (XMLObject attributeValue : attribute.getAttributeValues()) {

            Element attributeValueElement = attributeValue.getDOM();

            String value = attributeValueElement.getTextContent();

            values.add(value);

            if (attributeOverwriteId.equals(attribute.getFriendlyName())) {
                overwriteId = value;
            }

        }

        profile.addAttribute(attribute.getName(), values);

    }

    if (attributeOverwriteId != null) {
        profile.setId(overwriteId);
    }

    profile.addAttribute("externalAuthentication", credentials.getExternalAuthentication());

    return profile;
}

From source file:edu.cornell.mannlib.vitro.webapp.web.templatemodels.customlistview.CustomListViewConfigFile.java

private String parsePostprocessorName(Document doc) throws InvalidConfigurationException {
    Element element = getZeroOrOneElement(doc, TAG_POSTPROCESSOR);
    if (element == null) {
        return "";
    } else {/*from   w  ww.j  a v a  2  s  . c  o  m*/
        return element.getTextContent();
    }
}

From source file:com.github.fritaly.svngraph.Update.java

public Update(Element element) throws ParseException {
    validateElement(element, "path");

    this.kind = Kind.getKind(element.getAttribute("kind"));
    this.action = Action.getAction(element.getAttribute("action"));
    this.path = element.getTextContent();

    if (element.hasAttribute("copyfrom-path")) {
        this.copyFromPath = element.getAttribute("copyfrom-path");
    }/* w  w  w  .j  a va  2  s  .  c  om*/
    if (element.hasAttribute("copyfrom-rev")) {
        this.copyFromRev = Long.parseLong(element.getAttribute("copyfrom-rev"));
    }
    if (element.hasAttribute("text-mods")) {
        this.merge = Boolean.parseBoolean(element.getAttribute("text-mods"));
    }
}

From source file:eu.optimis.mi.aggregator.util.XmlUtil.java

public String getObjXml(String xml) {
    try {//from  w  w w . j  a v a2s . c  o m
        // Create a builder factory
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
        NodeList timestampList = doc.getElementsByTagName("metric_timestamp");
        for (int i = 0; i < timestampList.getLength(); i++) {
            Element ts = (Element) timestampList.item(i);
            String tsLangType = ts.getTextContent();
            try {
                long millis = 0;
                millis = Long.parseLong(tsLangType);
                Date udate = new Date(millis * 1000);
                String timestamp = DateFormatUtils.ISO_DATETIME_FORMAT.format(udate);
                ts.setTextContent(timestamp);
            } catch (NumberFormatException e) {
            }
        }
        String rs = xmlToString(doc);
        return rs;

    } catch (SAXException e) {
        return null;
    } catch (ParserConfigurationException e) {
        return null;
    } catch (IOException e) {
        return null;
    }
}

From source file:com.alibaba.citrus.service.form.support.AbstractValidatorDefinitionParser.java

@Override
protected final void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
    // parse id//from   w w  w.  j a va2s. co m
    attributesToProperties(element, builder, "id");

    // parse message
    String message = trimToNull(element.getAttribute("message"));

    if (message == null) {
        message = trimToNull(element.getTextContent());
    }

    if (message != null) {
        builder.addPropertyValue("message", message);
    }

    // attributes
    doParseAttributes(element, parserContext, builder);

    // element
    doParseElement(element, parserContext, builder);
}

From source file:com.norconex.importer.parser.impl.xfdl.XFDLParser.java

private void parseXML(Document doc, Writer out, ImporterMetadata metadata) throws IOException {

    // Grab the title
    NodeList xmlTitles = doc.getElementsByTagName("title");
    if (xmlTitles != null && xmlTitles.getLength() > 0) {
        Node titleItem = xmlTitles.item(0);
        if (titleItem instanceof Element) {
            metadata.addString("title", ((Element) titleItem).getTextContent());
        }//w w  w.  java  2s .c  om
    }

    boolean isEmpty = true;
    NodeList xmlFields = doc.getElementsByTagName("field");
    for (int i = 0; i < xmlFields.getLength(); i++) {
        if (xmlFields.item(i) instanceof Element) {
            NodeList children = xmlFields.item(i).getChildNodes();
            for (int j = 0; j < children.getLength(); j++) {
                Node childItem = children.item(j);
                if (childItem instanceof Element) {
                    Element tag = ((Element) childItem);
                    String tagName = tag.getTagName();
                    if ("value".equalsIgnoreCase(tagName)) {
                        isEmpty = writeValue(out, tag.getTextContent(), isEmpty);
                    }
                }
            }
        }
    }
}

From source file:org.carewebframework.shell.plugins.PluginXmlParser.java

/**
 * Parses out configuration settings for current property descriptor.
 * //from  w ww.j a va 2 s .c  om
 * @param property Root element
 * @param propertyBuilder Bean definition builder.
 */
private void parseConfig(Element property, BeanDefinitionBuilder propertyBuilder) {
    Element config = (Element) getTagChildren("config", property).item(0);

    if (config != null) {
        Properties properties = new Properties();
        NodeList entries = getTagChildren("entry", config);

        for (int i = 0; i < entries.getLength(); i++) {
            Element entry = (Element) entries.item(i);
            String key = entry.getAttribute("key");
            String value = entry.getTextContent().trim();
            properties.put(key, value);
        }

        propertyBuilder.addPropertyValue("config", properties);
    }
}

From source file:ImageEncode.java

public void decode(String filePath, Element node) throws IOException {
    final File imageFile = new File(filePath);
    final OutputStream os = new FileOutputStream(imageFile);
    String encodedImage = node.getTextContent();
    // String decoded = decode(encodedImage);
    // os.write(decoded);
    final Runtime runtime = Runtime.getRuntime();
    System.out.println("Free memory : " + runtime.freeMemory());
    String[] sei = encodedImage.split("\r\n");
    // System.out.println(encodedImage);
    System.out.println("Free memory : " + runtime.freeMemory());
    for (final String element : sei) {
        final byte[] byteImage = Base64.decodeBase64(element);
        try {//  ww  w.j a  v  a2s.c  o m
            os.write(byteImage);
        } catch (final FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    os.close();
    System.out.println("Free memory : " + runtime.freeMemory());
    encodedImage = null;
    sei = null;
    System.gc();
    System.out.println("Free memory : " + runtime.freeMemory());
}