XPath

reibi

Top Contributor
Habe beim auslesen eines XPAths das Problem das ich nicht unterscheiden kann, ob es den Wert gibt oder ob er einfach auf ""(lerer String) gesetzt ist.

Java:
        XPathExpression expr = xpath.compile(""//inventory/book[1]/@diesesAttributGibtsGarNicht"");
        String result = (String)expr.evaluate(doc, XPathConstants.STRING);

Bei dem Beispiel kommt sowas --> "" zurück. Also ein leerer String. Ich würde aber am liebsten null erwarten, das mir signalisiert, das dieser XPath nicht valiede ist. Wenn "" zurückkommt, muss ich eigentlich annehmen, das der XPath valide ist, aber nichts drin steht. Is aber leider nich so mit dem MisX.


Weiss da jemand wie ichs machen kann?

Gruss
 

reibi

Top Contributor
Hi Noctarius

Naja, das ist ja auch nur ein Ausschnitt von nem üblichen XPath-abzug.

ich mach mal was compilierungsfähiges:

Java:
import org.w3c.dom.Document;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;


public class TestXPath {
    public static void main(String[] args) throws Exception {
        File myXMLFile = new File("src/main/java/test/xp/books.xml");

        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true); // never forget this!

        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse(myXMLFile);

        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile("//inventory/book[1]/@diesesAttributGibtsGarNicht");
        String result = (String) expr.evaluate(doc, XPathConstants.STRING);

        System.out.println("\"" + result + "\"");
    } // end main()
} // end TestXPath

Kannste so ausführen mit Angabe eines XML-Files

Gruzss
 

reibi

Top Contributor
OK... passendes XML:


Dat ding heisst books.xml

[XML]
<inventory>
<book year="2000">
<title>Snow Crash</title>
<author>Neal Stephenson</author>
<publisher>Spectra</publisher>
<isbn>0553380958</isbn>
<price>14.95</price>
</book>

<book year="2005">
<title>Burning Tower</title>
<author>Larry Niven</author>
<author>Jerry Pournelle</author>
<publisher>Pocket</publisher>
<isbn>0743416910</isbn>
<price>5.99</price>
</book>

<book year="1995">
<title>Zodiac</title>
<author>Neal Stephenson</author>
<publisher>Spectra</publisher>
<isbn>0553573862</isbn>
<price>7.50</price>
</book>

<!-- more books... -->

</inventory>
[/XML]

Gruss
 

eRaaaa

Top Contributor
Anstelle dir den String zu holen könntest du dir den Node holen
Java:
Node result = (Node) expr.evaluate(doc, XPathConstants.NODE);

Das sollte null liefern. Wenn es das Attribut gibt, kannste` dir den Inhalt wiederum über
Code:
result.getTextContent()
holen
 

eRaaaa

Top Contributor
Mhm? Ok ich glaube jetzt auch, dass ich dich missverstanden habe :autsch:

Du willst doch ein Attribut von dem ersten Buch selektieren oder? Wenn es das Attribut gibt ,bzw allgemein der XPath stimmt(es eine Ergebnissmenge gibt) sollte es dir != null zurückgeben, ansonsten ist der Node eben null. Das war doch das was du wolltest oder nicht?
 

reibi

Top Contributor
Hi eRaaa

Also wenn der Pfad exiastiert, dannn soll es mir das ausgeben was drin steht, auch wenn ein leerer String drin steht. Wenn es den Pfad nicht gibt, dann soll er null zurückliefern.

gruss
 

eRaaaa

Top Contributor
Und was genau geht jetzt an dem von mir vorgeschlagenem Weg nicht?
Java:
		Node result = (Node) expr.evaluate(doc, XPathConstants.NODE);
		System.out.println("\"" + (result!=null?result.getTextContent():result) + "\"");

Auf dein Beispiel bezogen sollte es bei @year --> "2000" liefern. Gibt es den Pfad nicht, sollte "null" ausgegeben werden... Oder tuts das nicht? <-- das war eig. meine Frage ;/
 

reibi

Top Contributor
1.) Das hier : The method getTextContent() is undefined for the type Node

2.) Ist das n spezialweg für Attribute.
Wenn der Wert, den ich ziehen will, sich in nem ELEMENT befindet, geht dieser weg nicht.
Einen speziellen XPath will ich konfigurierbar machen, unabhängig von einem Element-Inhalt, Attribut oder Liste

Dafür ist ja XPath auch da oder?
gruss
 

reibi

Top Contributor
neee, mir is nämlich dank Euch ne andere Idee gekommen, folgendes ist wieder allgemeingültig:

Java:
        //------
        String result = (String) expr.evaluate(doc, XPathConstants.STRING);
        Node node = (Node) expr.evaluate(doc, XPathConstants.NODE);
        //------

        String returnValue = null;
        if (node != null) {
			returnValue=result;
		} 

        
        System.out.println(returnValue);

läuft astrein ;-)
 

Noctarius

Top Contributor
Java:
        //------
        Node node = (Node) expr.evaluate(doc, XPathConstants.NODE);
        //------
 
        String returnValue = null;
        if (node != null) {
            returnValue=node.getNodeValue();
        } 
 
        
        System.out.println(returnValue);

Du machst damit unnötig viele Selections.
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
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
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
P XPath und Namespaces XML & JSON 3
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
M XPATH und RSS (Problem namespaces) XML & JSON 7
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

Ähnliche Java Themen

Neue Themen


Oben