XPath und Namespaces

peez

Bekanntes Mitglied
Ich habe ein Dokument das folgendermaßen anfängt:

[XML]<?xml version="1.0" encoding="UTF-8"?>
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="XMP Core 4.4.0-Exiv2">
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about=""
xmlns:tiff="http://ns.adobe.com/tiff/1.0/"
xmlns:exif="http://ns.adobe.com/exif/1.0/"
xmlns:xmp="http://ns.adobe.com/xap/1.0/"
xmlns:aux="http://ns.adobe.com/exif/1.0/aux/"
xmlns:crs="http://ns.adobe.com/camera-raw-settings/1.0/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:photoshop="http://ns.adobe.com/photoshop/1.0/"
xmlns:xmpRights="http://ns.adobe.com/xap/1.0/rights/"
xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/"
xmlns:stRef="http://ns.adobe.com/xap/1.0/sType/ResourceRef#"
tiff:ImageWidth="4252"[/XML]

Wie würde ich denn jetzt z.B. an die Node <rdf:RDF...> kommen?

Wie ich schon über google gefunden habe, muss ich zuerst einen NamespaceContext definieren, der die verwendeten Namespaces kennt. Dazu habe ich mir diese kleine Klasse geschrieben:

Java:
private class MyNamespaceContext implements NamespaceContext {
    private Hashtable<String, String> ns = new Hashtable<String, String>();

    public String getNamespaceURI(String prefix) {
      String uri = ns.get(prefix);
      if (uri == null) {
        return XMLConstants.NULL_NS_URI;
      } else {
        return uri;
      }
    }
    
    public void add(String prefix, String namespaceURI) {
      ns.put(prefix, namespaceURI);
    }

    public String getPrefix(String namespaceURI) {
      Enumeration<String> keys = ns.keys();
      while (keys.hasMoreElements()) {
        String prefix = keys.nextElement();
        if (ns.get(prefix).equals(namespaceURI)) {
          return prefix;
        }
      }
      return null;
    }

    public Iterator getPrefixes(String namespaceURI) {
      // TODO Auto-generated method stub
      return null;
    }
  }

Die Node versuche ich dann so zu bekommen:
Java:
XPath xpath = XPathFactory.newInstance().newXPath();

    MyNamespaceContext nsCtx = new MyNamespaceContext();
    nsCtx.add("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
    nsCtx.add("x", "adobe:ns:meta/");
    nsCtx.add("tiff", "http://ns.adobe.com/tiff/1.0/");
    nsCtx.add("exif", "http://ns.adobe.com/exif/1.0/");
    nsCtx.add("xmp", "http://ns.adobe.com/xap/1.0/");
    nsCtx.add("aux", "http://ns.adobe.com/exif/1.0/aux/");
    nsCtx.add("crs", "http://ns.adobe.com/camera-raw-settings/1.0/");
    nsCtx.add("dc", "http://purl.org/dc/elements/1.1/");
    nsCtx.add("photoshop", "http://ns.adobe.com/photoshop/1.0/");
    nsCtx.add("xmpRights", "http://ns.adobe.com/xap/1.0/rights/");
    nsCtx.add("xmpMM", "http://ns.adobe.com/xap/1.0/mm/");
    nsCtx.add("stRef", "http://ns.adobe.com/xap/1.0/sType/ResourceRef#");
    
    xpath.setNamespaceContext(nsCtx);
    
    
    Node node =
        (Node) xpath.evaluate("/x:xmpmeta/rdf:RDF", myXmlDoc, XPathConstants.NODE);

Leider ist node immer null...

Was mach ich falsch?
 
B

bemar

Gast
Wenn du nicht an XPath hängst empfehle ich dir für XML Handling "JaxB".
Das wandelt dein XML in ein POJO um und du kannst z.B. mit xmpmeta.RDF.Description.xxxxxx an deine Werte rankommen.
Ist echt stressfreier ....

Gruß
Ben
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
M XPATH und RSS (Problem namespaces) XML & JSON 7
Kirby.exe Probleme mit XML XPATH Select XML & JSON 3
B XPath soll XML liefern XML & JSON 7
O XPATH Problem - Anfänger XML & JSON 4
M dom4j unberechtigte Invalid XPath expression XML & JSON 12
W XPath schreiben XML & JSON 1
P HTML XPath XML & JSON 1
S Ersatz für die veraltete Klasse XPath XML & JSON 1
C XPath: Alle Kinder in einer Schleife auslesen XML & JSON 2
P Xpath zugriff auf Attribute XML & JSON 3
P "XPath is deprecated" XML eclipse XML & JSON 3
G HtmlUnit XPath XML & JSON 5
D XPath-Query XML & JSON 2
I XPath Namensräume und auslesen einzelner Knoten XML & JSON 3
AMStyles XPATH Befehl SVG Java XML & JSON 2
G xpath: in Kindelement zwei Attribut-Werte auslesen XML & JSON 2
G xpath: Inhalt eines Kindelements auslesen XML & JSON 2
H ChildNode via XPath ansprechen XML & JSON 2
G JDOM - aus Children-Liste ein Child direkt ansprechen ohne XPath? XML & JSON 9
S Attribute von Elementen auslesen mit XPath XML & JSON 2
T Xpath & JDOM Element Pfad ausgeben XML & JSON 2
W XPath + Inhalt aus <td> XML & JSON 2
R XPath - frage zur Adressierung XML & JSON 3
reibi XPath XML & JSON 14
M xPath liefert leeres Nodeset XML & JSON 2
M Probleme mit XPath bei Java XML & JSON 5
W Element mit XPath @id ermitteln wenn nur ein teil der id bekannt ist XML & JSON 2
F XPath-Problem mit DOM4J XML & JSON 8
T XPath Problem: finden einer Node nach Attributswert XML & JSON 2
O XPATH gesucht XML & JSON 6
G XPATH-Frage XML & JSON 2
L Element manipulieren in einem DOMResult per XPath XML & JSON 10
G XPath - replace function funktioniert nicht XML & JSON 3
E XPATH-Ausdruck mit not XML & JSON 4
E Wieso liefert dieser XPATH-Ausdruck nicht die richtige Anzahl Treffer? XML & JSON 8
M XPath Ausdruck validieren XML & JSON 2
N XPath Adressierung XML & JSON 7
F XPath frage XML & JSON 4
B Optionaler XPath pfad XML & JSON 2
B XPath frage XML & JSON 2
T Frage zu XQuery (XQJ) / XPath XML & JSON 2
P XPath Problem XML & JSON 2
S Problem mit XPath XML & JSON 4
T Mit XPATH finden und dann änder XML & JSON 2
T EXSD auslesen mit XPath und JDOM XML & JSON 8
G XPath gesucht für Tag mit konkretem Content XML & JSON 2
A XPath Problem XML & JSON 2
C XPath in JDOM klappt nicht XML & JSON 2
T Prbolem XPath XML & JSON 2
X Xpath, alle Element die mit "user" beginnen auswäh XML & JSON 2
loadbrain XPath Problem XML & JSON 2
M brauche große XML-Datei für XPath-Tests XML & JSON 4
M JDOM und XPath, zu Element zugehörigen XPath-Ausdruck XML & JSON 5
F JDOM und XPath - Problem mit Namespace ohne Prefix XML & JSON 5
F hilfe bei xpath-ausdruck XML & JSON 2
K java + xpath -> performanceproblem XML & JSON 9
H Xpath kindelemente auslesen XML & JSON 2
S Xml zurück in xpath wandeln XML & JSON 8
G Frage zu XPath XML & JSON 2
flashfactor Filtern mittels XPATH XML & JSON 4
M XSL/XPath - Nur ersten Wert mit selben Attribut XML & JSON 4
M XPath Problem im Zusammenhang mit document() XML & JSON 2
M Frag zu xsl:when beziehungsweise XPath XML & JSON 3
F JDom und XPath XML & JSON 12
C xpath funktioniert nicht XML & JSON 5
P nochmal XPath :-) XML & JSON 22
P XPath . XML & JSON 2
T Rekursiver Verzeichniss Baum in XML & XPath XML & JSON 4
clemson xpath mit jdom XML & JSON 2
Wildcard xpath Parser XML & JSON 8
Rakshan Unmarshalling multiple namespaces with jaxb XML & JSON 0
M XML-Datei mit JAXB und 2 Namespaces XML & JSON 0
N Alle namespaces ins Root Element hinauf ziehen XML & JSON 2
N Alle namespaces ins Root Element hinauf ziehen XML & JSON 2
G Probleme mit Namespaces XML & JSON 5
D XPathAPI.selectSingleNode + Namespaces XML & JSON 4
D XML-file mit Eclipse erstellen + Namespaces XML & JSON 2
X XML/XHTML und Namespaces XML & JSON 2
K xml Datei mit JDOM erzeugen, Problem Namespaces XML & JSON 1

Ähnliche Java Themen

Neue Themen


Oben