Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between localname and qname?

Tags:

java

xml

sax

When using SAX to parse an XML file in Java, what is the difference between the parameters localname and qname in SAX methods such as startElement(String uri, String localName,String qName, Attributes attributes)?

like image 288
Bob Avatar asked Aug 23 '11 07:08

Bob


People also ask

What is QName in soap?

A QName object is a tuple that represents an XML qualified name. The tuple is composed of a namespace URI and the local part of the qualified name. In the QName parameter of the createService invocation, the local part is the service name, MyHelloService .

What is a QName in XML?

A QName, or qualified name, is the fully qualified name of an element, attribute, or identifier in an XML document. A QName concisely associates the URI of an XML namespace with the local name of an element, attribute, or identifier in that namespace.

What is javax XML namespace QName?

QName represents a qualified name as defined in the XML specifications: XML Schema Part2: Datatypes specification, Namespaces in XML, Namespaces in XML Errata. The value of a QName contains a Namespace URI, local part and prefix. The prefix is included in QName to retain lexical information when present in an javax.


2 Answers

The qualified name includes both the namespace prefix and the local name: att1 and foo:att2.

Sample XML

<root      xmlns="http://www.example.com/DEFAULT"      att1="Hello"      xmlns:foo="http://www.example.com/FOO"      foo:att2="World"/> 

Java Code:

att1

Attributes without a namespace prefix do not pick up the default namespace. This means while the namespace for the root element is "http://www.example.com/DEFAULT", the namespace for the att1 attribute is "".

int att1Index = attributes.getIndex("", "att1"); attributes.getLocalName(att1Index);  // returns "att1" attributes.getQName(att1Index);  // returns "att1" attributes.getURI(att1Index);  // returns "" 

att2

int att2Index = attributes.getIndex("http://www.example.com/FOO", "att2"); attributes.getLocalName(att2Index);  // returns "att2" attributes.getQName(att2Index);  // returns "foo:att2" attributes.getURI(att2Index);  // returns "http://www.example.com/FOO" 
like image 115
bdoughan Avatar answered Sep 22 '22 18:09

bdoughan


Generally speaking, localname is the local name, meaning inside the namespace. qname, or qualified name, is the full name (including namespace). For example, <a:b …> will have a localname b, but a qname a:b.

This is however very general, and settings-dependant. Take a look at the example at the end of this page for a more thorough example: example

like image 37
Eran Zimmerman Gonen Avatar answered Sep 23 '22 18:09

Eran Zimmerman Gonen