Java SAXParser: Different between `localName` and `qName`

10,454

Solution 1

As an example, I'm going to refer to the following XML sample:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="note">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="to" type="xs:string"/>
        <xs:element name="from" type="xs:string"/>
        <xs:element name="heading" type="xs:string"/>
        <xs:element name="body" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Namespace

A namespace is the logical container in which an element is defined. The XML Schema namespace (with uri: http://www.w3.org/2001/XMLSchema). In the above document, it is being referenced on line 2. XML document processing may occur using an XML parser which is either namespace-aware or not, but documents using namespaces will typically need to be parsed by namespace-aware parsers.

Namespaces are defined so that a) they can be cataloged by the parser and b) so that elements with the same name in different namespaces can exist in the same document without becoming ambiguously-defined.

Prefix

A prefix is the short-hand key used to refer to a namespace. In the above example, xs is used to refer to the XML Schema namespace.

Local Name (Part)

An element in a document has a name as it is defined in the namespace. In the above example, you can find schema, element, complexType, sequence, and element as local names. Local names can be ambiguous if you have multiple namespaces referenced in your document and one or more of those namespaces define elements with the same name.

Qualified Name (qName)

A qualified name consists of the prefix for the namespace (optionally, some implementations can use the namespace uri), followed by a :, followed by the element's local name. In the above example, you can find xs:schema, xs:element, xs:complexType, xs:sequence, and xs:element as qualified names. These names are unambiguous, and can be processed by the parser and validated.

Solution 2

Ryan's answer is excellent. The only other piece of information you need is that the exact details of what gets reported on the startElement event in SAX depend on various configuration settings of the SAX parser. Sadly, I don't have time to give the meticulous detail that Ryan has done.

Solution 3

In sax parser there are local name ,qname and namespace

qname is name of tag along with namespace while local name is only tag name. local name may be ambiguous but qname never.

Share:
10,454
hqt
Author by

hqt

Those who don't speak math are doomed to speak nonsense. My brief profile: https://github.com/hqt/profile

Updated on June 08, 2022

Comments

  • hqt
    hqt about 2 years

    In Java, Handler class contains method which name is startElement.this method has prototype:

    public void startElement(String uri, String localName, String qName, Attributes attributes)

    I have read on Oracle Java website, but I still not understand what different between localName and qName parameter.Here they explain:

    localName - The local name (without prefix), or the empty string if Namespace processing is not being performed. qName - The qualified XML 1.0 name (with prefix), or the empty string if qualified names are not available.

    In above definition, I don't know some concepts: prefix (prefix of what ?) Namespace

    Who can explain for me (as most simple as you can) about these parameter, please.

    thanks :)