Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top