Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
460 views
in Technique[技术] by (71.8m points)

java - What is the difference between localname and qname?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...