0

I am using xmlstreamreader in java to read attribute values and other data. This is the xml String:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><samlp:AuthnReques
t xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" AssertionConsumerServiceURL
="http://localhost:8080/consumer.jsp" **ID="abc"** **IssueInstant="2012-04-14T11:44:49
:796"** ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Version="
2.0">**<saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">http://loca
lhost:8080/saml/SProvider.jsp</saml:Issuer>**<Signature xmlns="http://www.w3.org/2
000/09/xmldsig#"><SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.or
g/2001/10/xml-exc-c14n#WithComments"/><SignatureMethod Algorithm="http://www.w3.
org/2000/09/xmldsig#rsa-sha1"/><Reference URI=""><Transforms><Transform Algorith
m="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/></Transforms><DigestM
ethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/><DigestValue>VzKYOu1g
ert3DDrNUSO1/Au3PGeD1PEyPuJeI2GO6ec=</DigestValue></Reference></SignedInfo><Sign
atureValue>k7hVlbsEhGW5ryelSbrwWWyJq3cdyDuVeQCOqRilbky8hEk/1sHI9DNOvOlPZ7OC9bI4d
EHm46R1
CDXoXkyOoXdq+3M/HbUakHM7eNvF5+j+NUXUX9dijb/rDzq05VNHcSIDXRpvMc1IRBremi0voVqX
ZuHRn+IBeD8hSK1LXsE=</SignatureValue></Signature></samlp:AuthnRequest>


Then I tried to read the attribute ID, IssueInstant and the element Issuer. all the 3 are highlighted(actually between **) in the above string. I have used the following code:

while(reader.hasNext()){
    reader.next();
    if(reader.getEventType() == XMLStreamReader.START_ELEMENT){

        if(reader.getLocalName().equals("AuthnRequest"))
        {
         String ns=reader.getNamespaceURI();
         System.out.println(ns);
         id=reader.getAttributeValue(ns,"ID");

         rec_instant=reader.getAttributeValue(ns,"IssueInstant");



        System.out.println("1"+id);
        System.out.println("2"+rec_instant);

        }
        else if(reader.getLocalName().equals("Issuer"))
        {
         rec_issuer=reader.getElementText();
         System.out.println("4"+reader.getElementText());
        }
    }
}


But I am getting the folowing output:

1null
2null
javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,436]
Message: parser must be on START_ELEMENT to read next text


What is the issue?

6
  • Which line throws the exception? Commented Apr 14, 2012 at 6:47
  • Is that your exact code? I could understand the exception if you'd got an extra brace somewhere... Commented Apr 14, 2012 at 6:51
  • @MarkByers : the line right after else if. Commented Apr 14, 2012 at 7:02
  • @JonSkeet : yes this is the exact code. why do you ask? Commented Apr 14, 2012 at 7:11
  • @Ashwin: For exactly the reason I stated: if you'd got an extra closing brace so that your else was actually associated with the first if rather than the second, it would explain everything. Commented Apr 14, 2012 at 7:19

2 Answers 2

2

You are using ns for the attributes, but the attributes are in fact in the null ns (they have no namespace). As for the exception, you are calling getElementText twice. This method is not a pure getter, it also advances the reader to the end element (as per its Javadoc).

Sign up to request clarification or add additional context in comments.

4 Comments

okay.. I tried with putting the "null" instead of ns. Still getting the same output.
@Ashwin: Did you use "null" instead of a null reference though? See my answer for working code.
@Marko Topolnik : thanks for pointing out the exception reason. I have corrected it. But still hte null value problem persists.
1

As Marko suggests, the exception is due to calling getElementText() twice in a row.

If I change this:

String rec_issuer=reader.getElementText();
System.out.println("4"+reader.getElementText());

to this:

String rec_issuer = reader.getElementText();
System.out.println("4" + rec_issuer);

then I get the following output:

urn:oasis:names:tc:SAML:2.0:protocol
1null
2null
4http://localhost:8080/saml/SProvider.jsp

If I also change the getAttributeValue calls to use null instead of ns, like this:

String id = reader.getAttributeValue(null,"ID");    
String rec_instant = reader.getAttributeValue(null,"IssueInstant");

I get:

urn:oasis:names:tc:SAML:2.0:protocol
1abc
22012-04-14T11:44:49:796
4http://localhost:8080/saml/SProvider.jsp

That's using your original XML.

6 Comments

can you just help me, out of curiosity. I'm reading the Javadoc and I don't see getAttributeValue. Where should I look?
@MarkoTopolnik: You're looking at XMLEventReader - it's in XMLStreamReader.
@JonSkeet : You are dead right about using reader.getElementText() twice. Thanks. But honestly using reader.getAttributeValue(null,"ID"); still prints null. I don't understand why.
@Ashwin: Well it didn't when I tried it - I wouldn't have posted that output without trying it. I can post a short but complete program to demonstrate that when I get home. Note that it would have made life easier if you'd formatted the XML in a cut-and-paste friendly way.
@JonSkeet :Sorry about that. But did you try the reader.getAttributeValue(null,"ID") on the first xmldoc or the second one.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.