xpath mit jdom

Status
Nicht offen für weitere Antworten.

clemson

Bekanntes Mitglied
hallo!

ich habe folgendes problem:

ich möchte aus einer XML datei mittels einer XPath Abfrage und JDOM, Elemente heraus holen...

XML-Datei:
Code:
<METS:mets xmlns:METS="http://www.loc.gov/METS/">
    <dmdSec ID="DCMD_ELEC">
        <mdWrap MIMETYPE="text/xml" MDTYPE="DC">
            <xmldata>
                <dc:creator xmlns:dc="http://purl.org/dc/elements/1.0/">Bovet, Theodor</dc:creator>
                <dc:title xmlns:dc="http://purl.org/dc/elements/1.0/">Von Mann zu Mann</dc:title>
                <dc:title xmlns:dc="http://purl.org/dc/elements/1.0/">Reifezeit</dc:title>
                <dc:title xmlns:dc="http://purl.org/dc/elements/1.0/">Vom Werden des Menschen</dc:title>
                <dc:title xmlns:dc="http://purl.org/dc/elements/1.0/">Vom männlichen Charakter</dc:title>
                <dc:title xmlns:dc="http://purl.org/dc/elements/1.0/">Von Meisterung und Verdrängung</dc:title>
                <dc:title xmlns:dc="http://purl.org/dc/elements/1.0/">Von den Mädchen</dc:title>
                <dc:title xmlns:dc="http://purl.org/dc/elements/1.0/">Klare Sicht</dc:title>
                <dc:title xmlns:dc="http://purl.org/dc/elements/1.0/">Nachkommenschaft</dc:title>
                <dc:title xmlns:dc="http://purl.org/dc/elements/1.0/">Was man auch noch wissen muss</dc:title>
                <dc:title xmlns:dc="http://purl.org/dc/elements/1.0/">Freiheit</dc:title>
            </xmldata>
        </mdWrap>
    </dmdSec>
</mets>

meine klasse MetsObject:
Code:
import java.io.*;
import java.io.IOException;

import java.util.List;

import org.jdom.*;
import org.jdom.xpath.*;
import org.jdom.input.*;
import org.jdom.output.*;

import org.xml.sax.SAXParseException;

import org.jaxen.jdom.JDOMXPath;

public class MetsObject
{

public static Document document;

public void parse (String path)
	{
		try {
			SAXBuilder saxbuilder = new SAXBuilder();
			saxbuilder.setIgnoringElementContentWhitespace(false);
			saxbuilder.setValidation(false);
			saxbuilder.setReuseParser(false);
			
	    	document = saxbuilder.build(new File(path));
	    	
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

public Object[] get_GRP( String abfrage )
	{
		try
			{
				
				XPath x = XPath.newInstance( abfrage );
				
							
		         	List ls = x.selectNodes( document );
				
				
                                if ( ls.isEmpty() | (ls.size()==0) )
				{
					return null;				
				}
				else
				{				
					return (Object[]) ls.toArray();
				}
			}
			catch (JDOMException ex)
			{
				ex.getMessage();					
				return null;
			}
			catch (ArrayIndexOutOfBoundsException ex)
			{
				ex.printStackTrace();					
				return null;
			}
						
		}
		else
		{
			return null;
		}
	}

}


und meine testklasse
Code:
public class testklasse
{
        public static void main(String[] args)
         {
			String query = new String("//dmdSec[@ID='DCMD_ELEC']/mdWrap/xmldata/node()/text()");
			
                       MetsObject mo = new MetsObject();
			
			mo.parse("C:\mets.xml");
        
			Object[] ob = new Object[mo.get_GRP( query ).length];

			ob = mo.get_GRP( query );

			System.out.println( ob[0].toString() );



			

          }
}


Normalerweise sollte er "Bovet, Theodor" ausgeben.

tut er aber nicht, er gibt stattdessen "[Text: Bovet, Theodor]" aus!!

wie komme ich auf den wert "Bovet, Theodor".

ich habe schon bei jdom.org-Documentation nachgeschlagen, und ich glaube die Lösung liegt bei einem org.jdom.filter.ContentFilter ....

könnt ihr mir weiterhelfen???
 
R

Roar

Gast
hmm gib mal auf der konsole: ob[0].getClass().getName() aus. ich nehme an da sind org.jdom.Text's in dem Object array gespeichert. versuche mal das ob[0] in ein Text objekt zu casten. dann kannst du mit getText() an "Bovet, Theodor" rankommen
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
G JDOM - aus Children-Liste ein Child direkt ansprechen ohne XPath? XML & JSON 9
T Xpath & JDOM Element Pfad ausgeben XML & JSON 2
T EXSD auslesen mit XPath und JDOM XML & JSON 8
C XPath in JDOM klappt nicht XML & JSON 2
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 JDom und XPath XML & JSON 12
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
S Attribute von Elementen auslesen mit XPath 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
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
G XPath gesucht für Tag mit konkretem Content XML & JSON 2
A XPath Problem 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
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
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
Wildcard xpath Parser XML & JSON 8
P JDOM oder SAX XML & JSON 3
Hijo2006 Grundsätzliches zu JDOM 2.x XML & JSON 1
W Kindelemente mit JDOM eines bestimmten Attributes auslesen XML & JSON 6
S JDOM XML Datei darstellen Fehler! XML & JSON 2
S JDom XML ein Element hinzufügen. XML & JSON 1
C JDOM: Value ohne Unterelemente auslesen XML & JSON 1
Z XML Datei mit JDOM einlesen und verarbeiten XML & JSON 6
J JDOM XMLOutputter getPrettyFormat() XML & JSON 3
H JDOM setAttribute Reihenfolge XML & JSON 3
B XML aus einer URL im Internet lesen (JDOM) XML & JSON 4
F Aus XML mit JDOM aus Elementattributen Werte rauslesen XML & JSON 4
R sax, stax, jdom, jaxb? List von Objekten speichern und laden XML & JSON 6
C xml validierung gegen xsd mit jdom: Fehlermeldung Cannot find the declaration of element ... XML & JSON 8
T Dom, Jdom, Sax XML & JSON 8
Helgon MalformedByteSequenceException und null JDOM Attribute XML & JSON 5
Helgon JDOM schreibt nicht XML & JSON 2
A XSl mit JDom auslesen XML & JSON 1
G xml, jdom "Entität wurde referenziert aber nicht deklariert." XML & JSON 4
A XSL und JDom XML & JSON 3
G jdom-Klassen in Eclipse importieren XML & JSON 2
S JDOM ohne XML Header erzeugen? XML & JSON 2
S JDOM nur Attribut Name XML & JSON 2
X JDom für SOAP Dateien geeignet? XML & JSON 3
M XML Parsen JDOM NullPointerEcxeption XML & JSON 7
S jdom hasChildren()? XML & JSON 1
T Frage zu getContent unter jdom XML & JSON 6
Z Tutorial für JDOM (XSD-Shema) XML & JSON 3
D jdom, xml, login-system XML & JSON 4
N JDOM: XML einlesen XML & JSON 4
O Parsen einer XML mit JDOM ohne Doctype Declaration XML & JSON 2

Ähnliche Java Themen

Neue Themen


Oben