XPath Problem: finden einer Node nach Attributswert

tonmann

Mitglied
Folgendes Problem: ich bräuchte als Rückgabe eigentlich ein Element, für die Verwendung in anderen Methoden. Ich wollte mir also eine Node zurück geben lassen, deren Name ich auslesen und über eine andere Methode ein Element mit dem gewonnenen Namen als ID heruassuchen kann.
Jedoch gibt es hier ständig Probleme beim erhalten einer passenden Node.
ein solches Element hat in der XML File folgende Struktur:
[XML]<id gername="" engname= ""/>[/XML]

Gesucht wird nach diesen beiden gername und engname, da diese in einer Swing jList angezeigt und darüber ausgewählt werden sollen.

Code:
Exception in thread "main" java.lang.ClassCastException: org.jdom.Document cannot be cast to org.w3c.dom.Node
	at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(Unknown Source)
	at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(Unknown Source)
	at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(Unknown Source)
	at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(Unknown Source)
	at test.Xmltest.searchObjectbyTrivial(Xmltest.java:173)
	at test.Xmltest.main(Xmltest.java:103)

Java:
/*
 * searches the document for the Object which has the english or german name specified, which is of the specified Object type.
 * @param type String the type of the Object to be searched
 * @param name the Name of the Object to be searched. this is not the ID
 * @return Element the Object as its xml Element representation
 */
public Element searchObjectbyTrivial(String type,String name) throws XPathExpressionException{
	Element work;
//	String query;
	
	
	String xpathE = new String("//Objects/" + type + "/*[@gername=" + name + "|@engname=" + name + "]");
	javax.xml.xpath.XPath xpath = XPathFactory.newInstance().newXPath();
	XPathExpression expr = xpath.compile(xpathE);
	System.out.println(doc.getContent().toString());
	work = doc.getRootElement();
	System.out.println(work.getContent().toString());
	//Object result = expr.evaluate(expr);
	Object result = expr.evaluate(doc);
	Node node = (Node) result;
	work = work.getChild("Objects").getChild(type).getChild(node.getNodeName());
	System.out.println(node.getNodeName());
	System.out.println(node.getLocalName());
	return work;
}
der Konstruktor der Klasse und die Fields sind hier zu sehen:
Java:
public class Xmltest {
	
	//Document document;
	Document doc;
	SAXBuilder sxbuild;
	Element rootelement;
	URL inputSource;
	URI path;
	XMLCreatorModule xmlmodule;
	
	public Xmltest(){
		
		
		
		
		sxbuild = new SAXBuilder();
		doc = null;
		
		try {
			inputSource = new URL("file:./Test oder so.xml");
		} catch (MalformedURLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		System.out.println(inputSource.getPath());
		
		try {
		//	document = sxbuild.build(inputSource);
			doc = sxbuild.build(inputSource);
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*rootelement = document.getRootElement();
		System.out.println(rootelement.getChildren().toString());
		XMLOutputter out = new XMLOutputter();
		try {
			out.output(document, System.out);
		} catch (IOException e) {
			e.printStackTrace();
		}
			System.out.println();
		*/
		
	}

Kann mir jemand sagen, wie ich den Code abändern muss, damit das funktioniert? ich sitze schon stunden daran, und bisher hat mir weder tutorial noch documentation oder API weitergeholfen...
Ich wollte allerdings nicht durch das jeweilige Dokument durchiterieren und das mit mit if-abfragen löchern, bis ich das element habe, wenns nicht schöner(einfacher) geht.
 
Zuletzt bearbeitet:

Noctarius

Top Contributor
[link]http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/xpath/XPathExpression.html#evaluate(org.xml.sax.InputSource, javax.xml.namespace.QName)[/link]

Code:
NodeList nodes = expression.evaluate(document, XPathConstants.NODESET);
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
O XPATH Problem - Anfänger XML & JSON 4
F XPath-Problem mit DOM4J XML & JSON 8
P XPath Problem XML & JSON 2
S Problem mit XPath XML & JSON 4
A XPath Problem XML & JSON 2
loadbrain XPath Problem XML & JSON 2
F JDOM und XPath - Problem mit Namespace ohne Prefix XML & JSON 5
M XPath Problem im Zusammenhang mit document() XML & JSON 2
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
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
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
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
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
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 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 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
so_ein_Komischer Problem mit Clean and Build XML & JSON 1
P JAXB-Problem XML & JSON 1
W Problem mit dem mit XML sortierung XML & JSON 2
S Jaxb Unmarshalling Problem XML & JSON 4
E einfaches Problem XML + XSD + jedit XML & JSON 2
S Xslt Problem XML & JSON 2
B JasperReport Problem in Runtime XML & JSON 2
S Problem with JAXB unmarshalling classes that have the same name in @XmlRootElement XML & JSON 2
R Problem bei: XML und XSL zu HTML XML & JSON 2
M Read / write Problem beim ByteStrom XML & JSON 2
M XML write Problem zweiter Ansatz XML & JSON 3
M XML read Problem XML & JSON 4
M XML write Problem XML & JSON 2
whitenexx Problem beim parsen von Facebook XML XML & JSON 3
M XML Unicode Problem XML & JSON 2
S XJC --> Java-Objects compile Problem XML & JSON 4
B JPA + JAXB Mapping Problem XML & JSON 2
G Problem beim schreiben von XML in eine File XML & JSON 2
S Encoding Problem XML & JSON 7
K JAXB und Maps -> Marshalling-Problem XML & JSON 6
B Problem beim löschen von ChildNodes aus einem XML-DOM XML & JSON 3
E JDOM - Problem beim Zusammenfügen zweier Dateien XML & JSON 2
M JExcelAPI (JXL) Encoding Problem XML & JSON 11
S DOM Parsen Problem mit HTML Sonderzeichen XML & JSON 4
A aus xml --> html Problem XML & JSON 3
Y stax Problem XML & JSON 3
slawaweis Problem mit XSLT (wahrscheinlich ein Bug in Java 6) XML & JSON 16
T Problem beim Parsen von Attribut xmlns="urn:com:test&qu XML & JSON 6
J Problem beim XML-Lesen XML & JSON 2
M Problem mit FOP in Java Programm XML & JSON 2

Ähnliche Java Themen

Neue Themen


Oben