Java SAXParser: Different between `localName` and `qName`
Asked Answered
J

4

13

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 :)

Judd answered 24/2, 2012 at 17:0 Comment(2)
Check out #7157855Isochronism
en.wikipedia.org/wiki/QNameMcdougal
U
31

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.

Unconscionable answered 24/2, 2012 at 18:2 Comment(5)
+1. shouldn't the next-to-last sentence in the Qualified Name paragraph end with "as qualified names" instead of "as local names"?Mccaffrey
yep, and that's why cut-and-paste development is bad. :DUnconscionable
Oh. thanks :) Your example is so simple and understand !!! after your example and explanation, I read another document abouth this and understand than ever.Judd
Here my question about this topic, too. Can you explain for me :) #9442093Judd
Brilliantly clear explanation. The word "namespace" is flung about left, right and centre: half the time I think a lot of people using it are not quite clear about what it means.Tomtit
D
2

QNames (Qualified Name) were introduced by XML Namespaces in order to be used as URI references. QName defines a valid identifier for elements and attributes. QNames are generally used to reference particular elements or attributes within XML documents and provide a mechanism for concisely identifying a {URI, local-name} pair. Namespaces can also be declared in the XML root element

Example:

<?xml version='1.0'?>
  <doc xmlns:x="http://example.com/ns/foo">
    <x:p/>
  </doc>  

The QName x:p is a concise, unambiguous name for the {URI, local-name} pair {"http://example.com/ns/foo", "p"}. Where doc is local name.

Java Analogy:

com.prem.java.Employee employee; //creating object using fully qualified name i.e. QName
Student student; //create an object using local name
Deathtrap answered 18/6, 2017 at 23:18 Comment(0)
K
1

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.

Kremlin answered 24/2, 2012 at 18:28 Comment(0)
F
1

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.

Foyer answered 23/10, 2013 at 14:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.