0
我想知道如何瀏覽這個XML文件以獲得一些值(如VersionXSD,Support,etx ...)。沒有命名空間,我可以做到這一點,但命名空間,我沒有得到任何結果。用命名空間讀取XML文件,使用java
<?xml version="1.0" encoding="UTF-8"?>
<ROOT Id="myRefId"
xsi:schemaLocation="urn:ir:si_my_xsd_07.xsd"
xmlns="urn:ir:se_pelmed" xmlns:nmsps="urn:siram:nmsps"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Horodatage>2013-03-05T12:00:00</Horodatage>
<VersionXSD>01.01</VersionXSD>
<BASE>
<nmsps:Numero>NUM-123-456</nmsps:Numero>
<nmsps:Type>TATA</nmsps:Type>
<nmsps:Support>PASTIS</nmsps:Support>
<nmsps:DateLimite>2013-03-04</nmsps:DateLimite>
<nmsps:Serial>0</nmsps:Serial>
<nmsps:BASEA>
<nmsps:NomFamille>Paul</nmsps:NomFamille>
<nmsps:Prenom>Hilaire</nmsps:Prenom>
<nmsps:DateNai>1979-10-11</nmsps:DateNai>
<nmsps:Sexe>Masculin</nmsps:Sexe>
</nmsps:BASEA>
<nmsps:BASEC Id="1">
<nmsps:Validite>2013-03-04</nmsps:Validite>
<nmsps:Sub>true</nmsps:Sub>
<nmsps:Stup>true</nmsps:Stup>
</nmsps:BASEC>
<nmsps:BASED Id="1">
<nmsps:Numero R="DD1">numero1</nmsps:Numero>
<nmsps:Categorie>Categorie1</nmsps:Categorie>
</nmsps:BASED>
<nmsps:BASED Id="2">
<nmsps:Numero R="DD2">numero2</nmsps:Numero>
<nmsps:Categorie>Categorie2</nmsps:Categorie>
</nmsps:BASED>
<nmsps:BASED Id="3">
<nmsps:Numero R="DD3">numero3</nmsps:Numero>
<nmsps:Categorie>Categorie3</nmsps:Categorie>
</nmsps:BASED>
</BASE>
</ROOT>
您必須爲XPath提供NamespaceContext。這個類沒有默認的impl,你必須自己實例化一個。請參閱http://stackoverflow.com/questions/8221652/how-to-retrieve-xml-data-using-xpath-which-contains-namespace-in-java/8221812#8221812 – flup 2013-03-13 19:37:01