question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`XMLStreamReader.getAttributeValue(null, localName)` does not ignore namespace URI

See original GitHub issue

i use woodstox-5.1.0 and jackson-2.7.7

Following quick xml-example shows that the function getAttributeValue(…) does not work as expected for resolving an attribute of an xml-element by its localName.

The xml used here is an example from: https://www.w3schools.com/xml/schema_schema.asp

        final String test =
                        "<note xmlns=\"https://www.w3schools.com\"\n" +
                        "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
                        "xsi:schemaLocation=\"https://www.w3schools.com note.xsd\">\n" +
                        "\n" +
                        "<to>Tove</to>\n" +
                        "<from>Jani</from>\n" +
                        "<heading>Reminder</heading>\n" +
                        "<body>Don't forget me this weekend!</body>\n" +
                        "</note> ";

        final InputStream is = new ByteArrayInputStream(test.getBytes(StandardCharsets.UTF_8));

        final XMLInputFactory staxInputFactory = XMLInputFactory.newInstance();
        final XMLStreamReader staxReader = staxInputFactory.createXMLStreamReader(is);

        staxReader.nextTag();

        // returns schemaLocation (correct!)
        final String attributeLocalName = staxReader.getAttributeLocalName(0);
        System.out.println(attributeLocalName);

        // returns value for schemaLocation (correct!)
        final String attributeValue = staxReader.getAttributeValue(0);
        System.out.println(attributeValue);

        // returns NULL (unexpected!)
        final String schemaLocation = staxReader.getAttributeValue(null, "schemaLocation");
        System.out.println(schemaLocation);

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Aug 21, 2018

@flappingeagle Ok thank you for including that information: I was not aware of Xerces-backed implementation (nor seen announcements). But class names do seem to suggest such existing. I don’t know if it might be based on old(er) Sun/Xerces implementation (“sjsxp”). Anyway that makes sense wrt your mention of Xerces. 😃

But back to the original question… since Javadoc does indeed state that null should mean “ignore namespace information” that does sound like a deviation from Stax specification.

I will try to see how easy it would be to fix.

Thank you for reporting this.

0reactions
cowtowncodercommented, Aug 23, 2018

Fixed. Attribute value lookup for null namespace case slightly less efficient as it needs linear scan (can’t use hash), but unlikely to be measurable difference. Will be included in 5.2.0, due out soon (will see if I can fix any other bugs).

Read more comments on GitHub >

github_iconTop Results From Across the Web

XMLStreamReader (Java Platform SE 8 ) - Oracle Help Center
Returns the normalized attribute value of the attribute with the namespace and localName If the namespaceURI is null the namespace is not checked...
Read more >
javax.xml.stream.XMLStreamReader.getNamespaceURI java ...
If the current event is a START_ELEMENT or END_ELEMENT this method returns the URI of the prefix or the default namespace. Returns null...
Read more >
JAXB: How to ignore namespace during unmarshalling XML ...
I have attempted to use this example in such a scenario and found that while it is capable of removing namespaces for the...
Read more >
JDK-6840792 sjsxp issue 70 XmlStreamReaderImpl ... - Bug ID
getAttributeValue namespace arg. not interpreted properly ... getAttributeValue(uri, localName) only works if namespace URI is passed as null, but not when ...
Read more >
XmlStreamReader Class | Apex Developer Guide
Returns the prefix of the current XML event or null if the event does not have a ... public String getAttributeValue(String namespaceUri, String...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found