0

My background in C#. I am facing behavioral difference while retrieving attribute count in JAVA compared to C#.

XML:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <book xmlns:v="urn:schemas-microsoft-com:vml" style="width:149.65pt" 
    xmlns:w="urn:schemas-microsoft-com:office:office">
    <v:object w:dxaOrig="1440" w:dyaOrig="1440">
    </v:object>
    </book> 

In C#, the ‘Xmlns’ tag is considered as the attribute and hence while retrieving the attribute count the value is 3 for the above Xml element ‘book’. Whereas in JAVA (as per my understating, correct me if am wrong) ‘Xmlns’ is not an attribute, it is used as namespace declaration and it is also considered as reserved XML pseudo-attribute to declare a namespace. So the attribute count for above case will be 1 in JAVA. I can retrieve the ‘xmlns’ tag as namespace count which is 2.

C# Code:

    for(int i=0;i<reader.AttributeCount;i++)
    {
    //Here the attribute count is 3
    }

Java Code:

    for (int i = 0; i < reader.getAttributeCount(); i++) {
    //Here the attribute count is 1  }

I have included a wrapper class for “XmlReader” to process attributes as in C# . Please refer the wrapper method I have used to retrieve attribute count to meet c# behavior.

Java:

    public int getAttributeCount() 
    {
    if (_reader.getEventType() == XMLStreamConstants.START_ELEMENT
            || _reader.getEventType() == XMLStreamConstants.ATTRIBUTE) {
        int i = 0;
        i = _reader.getAttributeCount();
        if (_reader.getNamespaceCount() > 0) {
            i += _reader.getNamespaceCount();

        }
        return i; //now the count will be 3.
        } else {
        return 0;
        }
        }

My concern is I would like to know how to read the element”book” attributes in order as in C#. Since ‘xmlns’ tag is considered as namespace, it is only possible to process namespace and attribute separately and it’s difficult to know which is in first while iterating the attribute count. Kindly let me know your suggestions in this?

Backs
  • 24,430
  • 5
  • 58
  • 85
Karthikk
  • 326
  • 2
  • 7
  • 1
    Why do you care what order they're in? – David Ehrmann Nov 16 '17 at 06:06
  • You should look into JAXB. Parsing XML this way is a nightmare. – David Ehrmann Nov 16 '17 at 06:07
  • This question was wrongly marked as a duplicate. The question, despite its title, is not about attribute order: it is about whether namespace declarations are considered to be attributes. – Michael Kay Nov 16 '17 at 08:45
  • @MichaelKay 'I would like to know how to read the element ”book” attributes in order'. – user207421 Nov 16 '17 at 23:52
  • @EJP yes I agree the question is thoroughly confused and confusing, but as I read it the essence is counting the number of attributes rather than delivering them in order. – Michael Kay Nov 17 '17 at 10:03
  • @MichaelKay My need not only to count a number of arguments but also need to retrieve all arguments including namespace in order (from "book" element). – Karthikk Nov 18 '17 at 07:25
  • Well, as others have said, attributes are unordered in XML so "in order" is a meaningless concept - there is no order. – Michael Kay Nov 18 '17 at 13:07

2 Answers2

0

xmlns is a reserved prefix/name in XML. As is explained here: http://www.w3.org/TR/xml-names/#xmlReserved

If you need to include those in you parse try handling the type

XmlStreamConstants.NAMESPACE 

as well

trappski
  • 1,066
  • 10
  • 22
0

You can't ask "are namespace declarations considered to be attributes in Java" because Java provides many libraries and models for processing XML, and they are all different.

In the DOM, which is available in slightly different form on many platforms including Java and C#, namespace declarations are treated as attributes. But the DOM is ancient and predates namespaces, and there are many more modern APIs for processing XML in Java. You example uses StAX; there is also SAX, JDOM2, XOM, etc etc. In most of the more modern models, namespaces and attributes are treated as separate things, so counting attributes will not include the namespace declarations.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164