Forums Register Login

Reading xml attributes in java

+Pie Number of slices to send: Send
I am tryin to read this xml file...



I basically want to know how to get this value folio="779" from the xml file in java...
This is what I have in my code...pretty basic, i am using an example online. I will be extracting about 5 values from that file, If you can help me on how to get one, I am sure I can work my logic to get the other values.

+Pie Number of slices to send: Send
Step 1... find elements named "Comprobante". You did that.

Step 2... cast them from Node to Element. You did that.

Step 3... get the "folio" attribute from that Element. But you have code which is looking for descendant elements named "certificado"? You don't need that. You already have an Element, just get whichever attribute you need. Hint: look at the API documentation for Element and see how to get attributes from an Element.
+Pie Number of slices to send: Send
Paul,
Thanks for the reply... I was able to get an attribute "folio" g, that is under <comprobante> tag. Now my question is do I need to repeat the same steps for attributes in different tags...
so I did this for TAG # 1, do I need to repeat the whole thing for TAG # 2, if the other value I want is under <conceptos>??
+Pie Number of slices to send: Send
You have to navigate to the data you want to extract, yes. (But what a question! How could the answer possibly be "No"?)
+Pie Number of slices to send: Send
Paul,
I have tried this and I am not being able to get the value I want... I am using the same .xml file!
+Pie Number of slices to send: Send
You are looking for an element named "Conceptos". The XML document you posted doesn't contain any elements named "Conceptos". It doesn't even contain that word anywhere. So I have no idea what you are trying to do. Perhaps you could explain why you thought that would be a good idea? I'm completely confused.
+Pie Number of slices to send: Send
Eduardo, I know this is not the answer you are looking for, but you would ease your life a lot if you used Jaxb.
+Pie Number of slices to send: Send
My feeling is that JAXB is vastly overrated and not applicable here.

To extract a single value from an XML document, the standard API is all you need. Use XPath if you want to minimize the number of statements at the cost of extra execution time.

Bill
+Pie Number of slices to send: Send
 

William Brogden wrote:My feeling is that JAXB is vastly overrated and not applicable here.



I'd say this is a matter of taste then. I'd certainly go for Jaxb.
+Pie Number of slices to send: Send
Paul,
You are certainly right... the xml file I posted is a different version than the one I am now trying... here it is to prove that I cant get that element from the file..


@Roberto
Thank you for the link, I will look at it and bother you if a have questions.
+Pie Number of slices to send: Send
Any help here....still stuck trying to read differente tags from the same file...
+Pie Number of slices to send: Send
JAXB was suggested earlier. Have you tried that ?
+Pie Number of slices to send: Send
 

Joanne Neal wrote:JAXB was suggested earlier. Have you tried that ?


I am struggling with it.. moving slowly!
+Pie Number of slices to send: Send
I dont think it will work because the .xml files that I am intendind to read are invoices that change month to month...So waht I am tryin gto do is get the information that I need from the invoices so that I can process it and add it to my DB. with JAXB I think i need to creat xml scheme definitions files for every file, seems a lot of work for what I am trying to achieve.
+Pie Number of slices to send: Send
So are you saying that the tag names and attribute names can change from month to month ?
+Pie Number of slices to send: Send
Joanne,
I think it can change only on the number of attribues....ie.

Maybe from month to month...instead of having 2 services being billed, there may be 3...but I dont think that would matter much, am I right...

That is why I am trying to stick to what I have, its just that I cant figure out how to get more than one tag name and attributes.
+Pie Number of slices to send: Send
Right Eduardo, but are they optional (and the set of attributes is well known) or they are pretty new? For instance, this month we had <Concepto cantidad="13"> and next month we can have <Concepto totallyUnexpectedAttribute="13">?
+Pie Number of slices to send: Send
Roberto,
The attributes are well known. I dont think the names would change, so far, they havent! I am comparing files...
+Pie Number of slices to send: Send
 

Eduardo Ponce de Leon wrote:Paul,
You are certainly right... the xml file I posted is a different version than the one I am now trying... here it is to prove that I cant get that element from the file.



You haven't proved that at all. Your code will certainly find those <Conceptos> elements. However it won't find the "cantidad" and "calidad" attributes because those elements don't have any such attributes. The <Concepto> elements do have those attributes, but your code doesn't look for them.

By the way I have edited the XML you posted so that it isn't seven screens wide. Unfortunately the code tags don't let the data wrap onto new lines like text does -- that's not your fault. Hopefully now it's possible to read what people have typed without having to scroll horizontally.
+Pie Number of slices to send: Send
Paul,
I get what you are saying...but I cant seem to figure out how to get attributes from difererent tagnames...
for example everything in <Conceptos> and <Impuestos>


that is my main issue...
+Pie Number of slices to send: Send
You just navigate to the element which contains the attribute, and then get the attributes from that element. Navigating to a parent element is useless.
+Pie Number of slices to send: Send
ok guys... with this code...

I am able to get this info...



but then... how go on and get this from inside <Emisor/>


any hints...
+Pie Number of slices to send: Send
I don't see why you need hints. You have posted code which does that.

So you have an Element object which represents your <Emisor> element, correct? And the <DomicilioFiscal> element is a child of that element, correct? You have already used the getElementsByTagName method earlier; why not use it again?
+Pie Number of slices to send: Send
ok...so I cant get the childs...I am stuck trying to get "cantidad,unidad".
I am able to get the name of <Concepto> but that is it...


this is my code...



PLEASE HELP!!!
1
+Pie Number of slices to send: Send


Don't use the == operator to compare the contents of two String objects. That just tells you whether the two Strings are the same object, whereas it's possible for many different String objects to contain the same data. Use the equals() method:
+Pie Number of slices to send: Send
Paul,
if I do as you say I never get inside my loop.
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 17240 times.
Similar Threads
Cannot read childnodes in XML - NEED HELP!
CMR problem in weblogic
WSSecurity, MTOM and TIBCO 5.6
CMR problem in weblogic
Sending XML over HTTP to Another Application
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 09:15:17.