File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Reading xml attributes in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reading xml attributes in java" Watch "Reading xml attributes in java" New topic
Author

Reading xml attributes in java

Eduardo Ponce de Leon
Ranch Hand

Joined: May 13, 2009
Posts: 99
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
Bartender

Joined: Oct 14, 2005
Posts: 18141
    
    8

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

Joined: May 13, 2009
Posts: 99
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
Bartender

Joined: Oct 14, 2005
Posts: 18141
    
    8

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

Joined: May 13, 2009
Posts: 99
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
Bartender

Joined: Oct 14, 2005
Posts: 18141
    
    8

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

Joined: Dec 28, 2007
Posts: 2247

Eduardo, I know this is not the answer you are looking for, but you would ease your life a lot if you used Jaxb.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
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

Java Resources at www.wbrogden.com
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2247

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

Joined: May 13, 2009
Posts: 99
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

Joined: May 13, 2009
Posts: 99
Any help here....still stuck trying to read differente tags from the same file...
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
JAXB was suggested earlier. Have you tried that ?


Joanne
Eduardo Ponce de Leon
Ranch Hand

Joined: May 13, 2009
Posts: 99
Joanne Neal wrote:JAXB was suggested earlier. Have you tried that ?

I am struggling with it.. moving slowly!
Eduardo Ponce de Leon
Ranch Hand

Joined: May 13, 2009
Posts: 99
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

Joined: Aug 05, 2005
Posts: 3169
    
  10
So are you saying that the tag names and attribute names can change from month to month ?
Eduardo Ponce de Leon
Ranch Hand

Joined: May 13, 2009
Posts: 99
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

Joined: Dec 28, 2007
Posts: 2247

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

Joined: May 13, 2009
Posts: 99
Roberto,
The attributes are well known. I dont think the names would change, so far, they havent! I am comparing files...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18141
    
    8

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

Joined: May 13, 2009
Posts: 99
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
Bartender

Joined: Oct 14, 2005
Posts: 18141
    
    8

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

Joined: May 13, 2009
Posts: 99
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
Bartender

Joined: Oct 14, 2005
Posts: 18141
    
    8

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

Joined: May 13, 2009
Posts: 99
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
Bartender

Joined: Oct 14, 2005
Posts: 18141
    
    8



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

Joined: May 13, 2009
Posts: 99
Paul,
if I do as you say I never get inside my loop.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading xml attributes in java
 
Similar Threads
CMR problem in weblogic
Sending XML over HTTP to Another Application
CMR problem in weblogic
Cannot read childnodes in XML - NEED HELP!
WSSecurity, MTOM and TIBCO 5.6