Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading xml attributes in java

 
Eduardo Ponce de Leon
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eduardo Ponce de Leon
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>??
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to navigate to the data you want to extract, yes. (But what a question! How could the answer possibly be "No"?)
 
Eduardo Ponce de Leon
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,
I have tried this and I am not being able to get the value I want... I am using the same .xml file!
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eduardo, I know this is not the answer you are looking for, but you would ease your life a lot if you used Jaxb.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13062
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eduardo Ponce de Leon
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eduardo Ponce de Leon
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any help here....still stuck trying to read differente tags from the same file...
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JAXB was suggested earlier. Have you tried that ?
 
Eduardo Ponce de Leon
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:JAXB was suggested earlier. Have you tried that ?

I am struggling with it.. moving slowly!
 
Eduardo Ponce de Leon
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So are you saying that the tag names and attribute names can change from month to month ?
 
Eduardo Ponce de Leon
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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">?
 
Eduardo Ponce de Leon
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roberto,
The attributes are well known. I dont think the names would change, so far, they havent! I am comparing files...
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eduardo Ponce de Leon
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eduardo Ponce de Leon
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Eduardo Ponce de Leon
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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:
 
Eduardo Ponce de Leon
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,
if I do as you say I never get inside my loop.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic