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?