Prezados estou com o seguinte código abaixo onde transformo uma String em XML e após tento efetuar uma leitura de algumas tags, porém só retrona null.
alguém pode ajudar a identificar o erro?
public static void main(String[] args) {
try {
String xmlstring = "<retEnviNFe xmlns=\"http://www.portalfiscal.inf.br/nfe\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" versao=\"2.00\"><tpAmb>2</tpAmb><verAplic>2.01</verAplic><cStat>103</cStat><xMotivo>Lote recebido com sucesso</xMotivo><cUF>29</cUF><dhRecbto>2011-07-31T11:22:00</dhRecbto><infRec><nRec>291500000126714</nRec><tMed>1</tMed></infRec></retEnviNFe>";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
InputSource inStream = new InputSource();
inStream.setCharacterStream(new StringReader(xmlstring));
Document doc1 = db.parse(inStream);
String numeroDoRecibo = doc1.getDocumentElement().getElementsByTagName("nRec").item(0).getNodeValue();
System.out.println(numeroDoRecibo);
} catch (Exception e) {
System.out.println(e.toString());
}
}
Cara, faltou o seguinte cabeçalho no seu xml:
<?xml version="1.0" encoding="UTF-8"?>
Sem adicionar isso no inicio de xmlstring, você não tem um arquivo xml e sim uma String comum.
Prezado
Este é o XML que recebo de retorno da SEFAZ, mas mesmo add a linha da informação do XML o mesmo não resolveu.
Se possivel faça o teste pra mim executando o metodo main acima, para ver que o resultado será null.
<?xml version="1.0" encoding="UTF-8"?>
<retEnviNFe xmlns="http://www.portalfiscal.inf.br/nfe"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
versao="2.00">
<tpAmb>2</tpAmb>
<verAplic>2.01</verAplic>
<cStat>103</cStat>
<xMotivo>Lote recebido com sucesso</xMotivo>
<cUF>29</cUF>
<dhRecbto>2011-07-31T11:22:00</dhRecbto>
<infRec>
<nRec>291500000126714</nRec>
<tMed>1</tMed>
</infRec>
</retEnviNFe>
Você deve pegar primeiramente o nó infRec para ter acesso à nRec. Rode o seguinte código:
[code]import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class XMLTest {
public static void main(String[] args) {
try {
String xmlstring = “<?xml version=\"1.0\" encoding=\"UTF-8\"?><retEnviNFe xmlns=“http://www.portalfiscal.inf.br/nfe” xmlns:xsd=“http://www.w3.org/2001/XMLSchema” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” versao=“2.00”>22.01103Lote recebido com sucesso292011-07-31T11:22:002915000001267141”;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
InputSource inStream = new InputSource();
inStream.setCharacterStream(new StringReader(xmlstring));
Document doc1 = db.parse(inStream);
doc1.getDocumentElement().normalize();
NodeList nodeList = doc1.getElementsByTagName(“infRec”);
Node node = nodeList.item(0);
Element element = (Element)node;
Node nRec = element.getElementsByTagName(“nRec”).item(0);
System.out.println(nRec.getChildNodes().item(0).getNodeValue());
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
[/code]
Essa linha pega o valor armazendado pelo nó:
nRec.getChildNodes().item(0).getNodeValue()
Agora foi…Muito obrigado mesmo…
bampia
#6
Resolveu meu problema também.