Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Java in General
Cannot read childnodes in XML - NEED HELP!
Eduardo Ponce de Leon
Ranch Hand
Posts: 108
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I have an xml file and want to read certain tags and attributes..
This is my file
<?xml version="1.0" encoding="UTF-8"?> <Comprobante certificado="MIIEWjCCA0KgAwIBAgIUMDAwMDEwMDAwMDAxMDIxNjAwMjcwDQYJKoZIhvcNAQEFBQAwggE2MTgwNgYDVQQDDC9BLkMuIGRlbCBTZXJ2aWNpbyBkZSBBZG1pbmlzdHJhY2nDs24gVHJpYnV0YXJpYTEvMC0GA1UECgwmU2VydmljaW8gZGUgQWRtaW5pc3RyYWNpw7NuIFRyaWJ1dGFyaWExHzAdBgkqhkiG9w0BCQEWEGFjb2RzQHNhdC5nb2IubXgxJjAkBgNVBAkMHUF2LiBIaWRhbGdvIDc3LCBDb2wuIEd1ZXJyZXJvMQ4wDAYDVQQRDAUwNjMwMDELMAkGA1UEBhMCTVgxGTAXBgNVBAgMEERpc3RyaXRvIEZlZGVyYWwxEzARBgNVBAcMCkN1YXVodGVtb2MxMzAxBgkqhkiG9w0BCQIMJFJlc3BvbnNhYmxlOiBGZXJuYW5kbyBNYXJ0w61uZXogQ29zczAeFw0xMDExMDgxODM2MTdaFw0xMjExMDcxODM2MTdaMIH6MTQwMgYDVQQDEytURUNOT0xPR0lBUyBERSBJTkZPUk1BQ0lPTiBBTUVSSUNBIFNBIERFIENWMTQwMgYDVQQpEytURUNOT0xPR0lBUyBERSBJTkZPUk1BQ0lPTiBBTUVSSUNBIFNBIERFIENWMTQwMgYDVQQKEytURUNOT0xPR0lBUyBERSBJTkZPUk1BQ0lPTiBBTUVSSUNBIFNBIERFIENWMSUwIwYDVQQtExxUSUEwNTA0MDgzNDIgLyBST0FBNzIxMjI3MlY5MR4wHAYDVQQFExUgLyBST0FBNzIxMjI3SFZaREROMTYxDzANBgNVBAsTBlVOSURBRDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAptYvZ5/a/z/s8YqeTd6LldcrbcZz69d3xtjtkxGR1NEl+w6tV2tUHI5+qNvbG1UHudAfKkjluVjvtgKCczgLZV8/dEYo42Pp8dmRmwLl6z1TFCBmeWN0XnjyounK8iH4tdHobD/PMxlDN09RFx2yeVxcaQCQFEuzN9izjbOb3bcCAwEAAaMdMBswDAYDVR0TAQH/BAIwADALBgNVHQ8EBAMCBsAwDQYJKoZIhvcNAQEFBQADggEBABTuUf/CdlmVCI/UF9YG5pqt203pu8vXckNAJT9JKaEh04MrrzXxm+lJwucTxDUk53ci1+kcHX48MsafCZiZEyw6uSNVAlW0/WFkmiia/vFncoRUWpizawI6R12vodl4uzc5zAJgO4FjQKQbm/nvtVpgBvuC0wTo/xpueA5gT7HyvIyQXfBKVJ99DNLDYyZSCI87q8YWQUl4WwETWfZ9amKR7qB+sQ1YroJo8W5VV/eCs/fksnr6TdHgjZMyoIaVtwt7JACc3odYoLtimVRgktQtT09ZcCmMYn6vGLOkDot32yx9u6KOjqB6fSGbZB4WSdv6QHQLcdA/hxk+FakUuQI=" xmlns:cfdi="http://www.sat.gob.mx/cfd/3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0" serie="UNICA" folio="779" fecha="2011-12-05T18:24:42" sello="ofKFKg1/FYM80g0DpoNm50KKs2H5p07kvQNNet75C/8GisFVgwwIvHNTTBNE/Q4eVfDERVAIGuRWoZ7VLMOrEb0MdeM44oHWnZfczH4T9z9LB4tHuMNHE79JjPhpePc7H4iC/kzdcGcCoKCwlhaMtfqr7LQkoACybmIcYamWg9g=" formaDePago="PAGO EN UNA SOLA EXHIBICION" noCertificado="00001000000102160027" condicionesDePago="EFECTOS FISCALES AL PAGO" subTotal="5123.23" total="5942.95" Moneda="USD" tipoDeComprobante="ingreso" xsi:schemaLocation="http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv3.xsd"> <Emisor rfc="TIA050408342" nombre="TECNOLOGIAS DE INFORMACION AMERICA S.A. DE C.V."> <DomicilioFiscal calle="Montecito" noExterior="38" noInterior="P 32-5" colonia="Napoles" localidad="Distrito Federal" municipio="Distrito Federal" estado="México" pais="Mex." codigoPostal="03810" /> </Emisor> <Receptor rfc="TEX9302097F3" nombre="TIENDAS EXTRA S.A. DE C.V."> <Domicilio calle="JAVIER BARROS SIERRA" noExterior="555" noInterior="PISO 2" colonia="ZEDEC ED. PLAZA SANTA FE" municipio="MEXICO" estado="DISTRITO FEDERAL" pais="MEXICO" codigoPostal="01210" /> </Receptor> <Conceptos> <Concepto cantidad="1" unidad="Servicio" descripcion="Servicios Administrados, equipamiento central para enlaces VPN voz y datos de oficinas regionales COMEXTRA en: - KIO NETWORKS" valorUnitario="1522.36" importe="1522.36" /> <Concepto cantidad="13" unidad="Servicio" descripcion="Servicios Administrados, equipamiento remoto para enlaces VPN voz y datos de oficinas regionales COMEXTRA en: - Puebla - Mérida - Aguascalientes - Cancún - Ciudad Juárez - Chihuahua - Tuxtla Gutiérrez - Villahermosa - Hermosillo - CEDIS Tijuana - COMEXTRA Tijuana - Guadalajara - Morelia NO. DE PROVEEDOR 181077 "TECNOLOGIAS DE INFORMACION AMERICA SA DE CV" SOCIEDAD 7502 ORGANIZACIÓN DE COMPRAS 1100" valorUnitario="276.99" importe="3600.87" /> </Conceptos> <Impuestos totalImpuestosTrasladados="819.72"> <Traslados> <Traslado impuesto="IVA" tasa="16.00" importe="819.72" /> </Traslados> </Impuestos> <Complemento> <tfd:TimbreFiscalDigital version="1.0" UUID="8A2CFD3A-298B-4A6F-BB3C-8CB23604B1EB" FechaTimbrado="2011-12-05T18:24:44" selloCFD="ofKFKg1/FYM80g0DpoNm50KKs2H5p07kvQNNet75C/8GisFVgwwIvHNTTBNE/Q4eVfDERVAIGuRWoZ7VLMOrEb0MdeM44oHWnZfczH4T9z9LB4tHuMNHE79JjPhpePc7H4iC/kzdcGcCoKCwlhaMtfqr7LQkoACybmIcYamWg9g=" noCertificadoSAT="00001000000102629688" selloSAT="NVoNxUVt756vu6SrwURc2lAZMHVVwcEHfPrlXJKYQzXhlQDlU6uvCZRE9poBE7AfmyTDO2I0/FwZ2KbJgmDK1Bn4VVdYnuAbz2lf/tBUST/SNAQcJGsloJkQfEwJwgpk7Lbh0a+TuW/aKUscbwxBoNOMALLRORGEnJNTgf5eTwg=" xsi:schemaLocation="http://www.sat.gob.mx/TimbreFiscalDigital http://www.sat.gob.mx/TimbreFiscalDigital/TimbreFiscalDigital.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tfd="http://www.sat.gob.mx/TimbreFiscalDigital" /> </Complemento> </Comprobante>
Basically, what i cant figure out is how to read the attribute <Concepto> and others from inside<Conceptos>
<Conceptos> <Concepto cantidad="1" unidad="Servicio" descripcion="Servicios Administrados, equipamiento central para enlaces VPN voz y datos de oficinas regionales COMEXTRA en: - KIO NETWORKS" valorUnitario="1522.36" importe="1522.36" /> <Concepto cantidad="13" unidad="Servicio" descripcion="Servicios Administrados, equipamiento remoto para enlaces VPN voz y datos de oficinas regionales COMEXTRA en: - Puebla - Mérida - Aguascalientes - Cancún - Ciudad Juárez - Chihuahua - Tuxtla Gutiérrez - Villahermosa - Hermosillo - CEDIS Tijuana - COMEXTRA Tijuana - Guadalajara - Morelia NO. DE PROVEEDOR 181077 "TECNOLOGIAS DE INFORMACION AMERICA SA DE CV" SOCIEDAD 7502 ORGANIZACIÓN DE COMPRAS 1100" valorUnitario="276.99" importe="3600.87" /> </Conceptos>
In my codeI am able to get <Conceptos> but just not able to read its childs... This is my code.
Document document = builder.parse(files[w]); Node root = document.getDocumentElement(); Node currentNode = null; NodeList nodeList = root.getChildNodes(); // ROOT System.out.println("Root Node:" + root.getNodeName()); if (root.getNodeName().equals("Comprobante")) { for (int i = 0; i < nodeList.getLength(); i++) { nodeList = document.getElementsByTagName("Comprobante"); NamedNodeMap child = document.getAttributes(); if (child != null) { System.out.println(child.item(0)); } for (int s = 0; s < nodeList.getLength(); s++) { Node fstNode = nodeList.item(s); if (fstNode.getNodeType() == Node.ELEMENT_NODE) { Element fstElmnt = (Element) fstNode; // System.out.println("Folio,Fecha"); System.out.println(fstElmnt.getAttribute("folio") + "-" + fstElmnt.getAttribute("fecha")); } } } nodeList = root.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { currentNode = nodeList.item(i); // System.out.println(currentNode.getAttributes().item(i).getNodeName()); if (currentNode.getNodeName() == "Conceptos") { if (currentNode.getAttributes() != null) { NodeList subList = currentNode.getChildNodes(); System.out.println(subList.item(0).getNodeName()); System.out.println(subList.item(0).getNodeValue()); } } } }
PLEASE ANY POINTERS WOULD BE GREATLY APPRECIATE IT.
Joanne Neal
Rancher
Posts: 3742
16
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
KeepItDown
Joanne
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I would use the org.w3c.dom methods for Element -
use getElementsByTagName( "Conceptos" )
with one of those Elements getElementsByTagName("Concepto")
with a Concepto Element getAttribute("whatever")
Harold's Processing XML with
Java
book is available free online, just search. It is loaded with practical examples.
Bill
Eduardo Ponce de Leon
Ranch Hand
Posts: 108
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
William,
Thanks for the reply... can I get a link, please!
Claudiu Chelemen
Ranch Hand
Posts: 75
I like...
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Well, have you tried getting them directly?
NodeList conceptos = doc.getElementsByTagName("Concepto");
Also, from what I can see, your second concepto has some quotes in the
descripcion
attribute, that should be escaped.
Claudiu
Joanne Neal
Rancher
Posts: 3742
16
posted 12 years ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
Also, as Paul told you
here
, don't use == to compare strings. Use the String.equals method.
Joanne
Eduardo Ponce de Leon
Ranch Hand
Posts: 108
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Claudiu, William;
You have been of great help actually with constructive comments, than you very much! I was able to solve my problem.
Do the next thing next. That’s a pretty good rule. Read the tiny ad, that’s a pretty good rule, too.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
CMR problem in weblogic
compiling error. Please help
CMR problem in weblogic
Place Association
BufferedReader is not Serializable?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...