xml属性中"本地名称"和"限定名称"之间的差异

mic*_*ael 13 java xml

你能帮我理解一下xml属性中'local name'和'qualified name'之间的区别吗?来自http://developer.android.com/reference/org/xml/sax/Attributes.html:

/** Look up an attribute's local name by index. */
abstract String getLocalName(int index)

/** Look up an attribute's XML qualified (prefixed) name by index. */    
abstract String getQName(int index)
Run Code Online (Sandbox Code Playgroud)

在这个例子中,

<anelement attr1="test" attr2="test2"> </anelement>
Run Code Online (Sandbox Code Playgroud)

有什么区别?

bdo*_*han 13

限定名称包括命名空间前缀和本地名称:att1foo:att2.

示例XML

<root 
    xmlns="http://www.example.com/DEFAULT" 
    att1="Hello" 
    xmlns:foo="http://www.example.com/FOO" 
    foo:att2="World"/>
Run Code Online (Sandbox Code Playgroud)

Java代码:

ATT1

没有名称空间前缀的属性不会选择默认名称空间.这意味着当root元素"http://www.example.com/DEFAULT"的名称空间是,att1属性的名称空间是"".

int att1Index = attributes.getIndex("", "att1");
attributes.getLocalName(att1Index);  // returns "att1"
attributes.getQName(att1Index);  // returns "att1"
attributes.getURI(att1Index);  // returns ""
Run Code Online (Sandbox Code Playgroud)

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"
Run Code Online (Sandbox Code Playgroud)


Jon*_*eet 7

本地名称是不受命名空间限定的名称.完全限定的名称包括名称空间(如果有).

可能值得阅读有关XML名称W3C建议以获取完整的详细信息.

基本上,如果您xmlns的XML文件中没有任何位置,则可能不需要担心命名空间.如果你这样做有命名空间,你可能会想建立一个完全合格的名称,元素名称等检查时

请注意,根据我的经验,属性通常不太可能使用名称空间而不是元素.