我解析一個簡單的XML並嘗試獲取nodeNames。在此XML的某些變體中,某些nodeNames具有名稱空間前綴「mets:」。我試圖匹配所有「fptr」元素,不管它們是否有一個mets-prefix。getNodeName,getLocalName不返回期望值
這裏是XML的例子,同時包含簡單FPTR元素和一些前綴:
<mets:structMap xmlns:mets="http://www.loc.gov/METS/" xmlns="http://www.loc.gov/METS/" TYPE="logical" ID="DTL1">
<div ORDER="1" LABEL="Alle Scans" TYPE="first level" ID="DTL2">
<div ORDER="1" LABEL="1" TYPE="Seite" ID="DTL3">
<mets:fptr FILEID="FID00000020" ID="DTL21"/>
</div>
<div ORDER="2" LABEL="2" TYPE="Seite" ID="DTL4">
<mets:fptr FILEID="FID00000021" ID="DTL22"/>
</div>
</div>
<div ORDER="1" LABEL="Hauptdokument - pdf" TYPE="entry" ID="DTL5">
<fptr FILEID="FID1a" ID="DTL11"/>
</div>
</mets:structMap>
而且這裏有一個簡單的解析程序應打印出的元素名稱和所有元素的NS-前綴:
package at.ac.onb.zid.dtlcontent.test;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class structMapTest {
public static void main(String args[]) throws ParserConfigurationException, SAXException, IOException {
File fXmlFile = new File("src/test/resources/teststructmap.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList elemList = doc.getElementsByTagName("*");
for(int i = 0; i<elemList.getLength();i++) {
Node n = elemList.item(i);
System.out.println("nodeName=" + n.getNodeName());
if(n instanceof Element) {
Element e = (Element) n;
String eID = e.getAttribute("ID");
String nsPrefix = e.getPrefix();
String eLN = e.getLocalName();
String eNSURI = e.getNamespaceURI();
System.out.println(" ID=" + eID);
System.out.println(" prefix=" + nsPrefix);
System.out.println(" localName=" + eLN);
System.out.println(" nsURI=" + eNSURI);
System.out.println("");
}
}
}
}
這是它打印出來,但:
nodeName=mets:structMap
ID=DTL1
prefix=null
localName=null
nsURI=null
nodeName=div
ID=DTL2
prefix=null
localName=null
nsURI=null
nodeName=div
ID=DTL3
prefix=null
localName=null
nsURI=null
nodeName=mets:fptr
ID=DTL21
prefix=null
localName=null
nsURI=null
nodeName=div
ID=DTL4
prefix=null
localName=null
nsURI=null
nodeName=mets:fptr
ID=DTL22
prefix=null
localName=null
nsURI=null
nodeName=div
ID=DTL5
prefix=null
localName=null
nsURI=null
nodeName=fptr
ID=DTL11
prefix=null
localName=null
nsURI=null
一前綴值爲空。我預計前兩個fptr前綴(ID = DTL21和DTL22)是「mets」。
與localName相同:我期望所有fptr-localNames都是「fptr」,而不是全部爲空。類似於命名空間-URI。
我在這裏錯過了什麼?
非常感謝。我懷疑像這樣的事情,但甚至不知道從哪裏開始看......解決了它。 – jackthehipster