null Rückgabe bei XPath

mkoeni1

Erfahrenes Mitglied
Hallo Leute,

ich hänge jetzt schon länger am gleichen Problem fest. Meine Frage: Wieso bekomme ich mit folgendem XPathExpression:
Code:
//field_of_study[@study_name='Informatik']/writer[@*]

null
null

zurück ? Eigentlich will ich die "writer_name" Attribute zurückbekommen des study_name="Informatik".

Und mit einem XPathExpression, auf die gleiche Datei angewendet:
Code:
//@writer_name

ein sinnvolle Ausgabe:

Meister Eder
Pumuckl
Max
Moritz



Mein Script sieht folgendermaßen aus:
Code:
public class XPathExamplenew {

  public static void main(String[] args) 
   throws ParserConfigurationException, SAXException, 
          IOException, XPathExpressionException {
      
    // FileReader file = new FileReader("src/xml/books.xml");

    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true); // never forget this!
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse("src/xml/survey_all.xml");

    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr = xpath.compile("//@writer_name");

    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    for (int i = 0; i < nodes.getLength(); i++) {
        System.out.println(nodes.item(i).getNodeValue()); 
    }

  }

}

Kann mir das jmd. verständlich erklären ?


Gruß
Matze


Die XML-Datei, sieht so aus:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Beeinhaltet  alle Umfragen -->
<survey_all xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="survey_all.xsd">
	<field_of_study study_name="Informatik">
		<writer writer_name="Meister Eder">
			<survey survey_name="Numerik" SID="nm2007">
				<authentication_modus>rsa</authentication_modus>
				<date_start>2006-12-11</date_start>
				<time_start>12:00:00</time_start>
				<date_end>2006-12-11</date_end>
				<time_end>12:00:00</time_end>
				<contact_person>
					<contact_gender>1</contact_gender>
					<contact_name>Manni</contact_name>
					<contact_email>1@2.de</contact_email>
					<contact_telefon>12345</contact_telefon>
				</contact_person>
			</survey>

			<survey survey_name="Automatentheorie" SID="afs07">
				<authentication_modus>rsa</authentication_modus>
				<date_start>2006-12-11</date_start>
				<time_start>12:00:00</time_start>
				<date_end>2006-12-11</date_end>
				<time_end>12:00:00</time_end>
				<contact_person>
					<contact_gender>1</contact_gender>
					<contact_name>Fritz</contact_name>
					<contact_email>1@2.de</contact_email>
					<contact_telefon>12345</contact_telefon>
				</contact_person>
			</survey>
		</writer>
		<writer writer_name="Pumuckl">
			<survey survey_name="MPPL" SID="mppl07">
				<authentication_modus>open</authentication_modus>
				<date_start>2006-12-11</date_start>
				<time_start>12:00:00</time_start>
				<date_end>2006-12-11</date_end>
				<time_end>12:00:00</time_end>
				<contact_person>
					<contact_gender>1</contact_gender>
					<contact_name>Klaus</contact_name>
					<contact_email>1@2.de</contact_email>
					<contact_telefon>12345</contact_telefon>
				</contact_person>
			</survey>
		</writer>
	</field_of_study>
        
        
           <field_of_study
		study_name="Master of Science">
		<writer writer_name="Max">
			<survey survey_name="Business Module" SID="bm">
				<authentication_modus>non</authentication_modus>
				<date_start>2007-01-01</date_start>
				<time_start>19:00:00</time_start>
				<date_end>2007-12-24</date_end>
				<time_end>19:00:00</time_end>
				<contact_person>
					<contact_gender>1</contact_gender>
					<contact_name>Famous</contact_name>
					<contact_email>1@2.de</contact_email>
					<contact_telefon>12345</contact_telefon>
				</contact_person>
			</survey>
		</writer>
		<writer writer_name="Moritz">
			<survey survey_name="Vision Therapy" SID="vtherapy">
				<authentication_modus>non</authentication_modus>
				<date_start>2007-01-01</date_start>
				<time_start>19:00:00</time_start>
				<date_end>2007-12-24</date_end>
				<time_end>19:00:00</time_end>
				<contact_person>
					<contact_gender>0</contact_gender>
					<contact_name>contact woman</contact_name>
					<contact_email>2@3.de</contact_email>
					<contact_telefon>12345</contact_telefon>
				</contact_person>
			</survey>
                 </writer>
	</field_of_study>

	<field_of_study study_name="Technisches Hilfswerk">
		<writer writer_name="CAD Professor">
			<survey survey_name="3D-CAD" SID="3d-cad">
				<authentication_modus>public</authentication_modus>
				<date_start>2007-01-01</date_start>
				<time_start>19:00:00</time_start>
				<date_end>2007-12-24</date_end>
				<time_end>19:00:00</time_end>
				<contact_person>
					<contact_gender>1</contact_gender>
					<contact_name>TR Kontakt</contact_name>
					<contact_email>1@2.de</contact_email>
					<contact_telefon>01235</contact_telefon>
				</contact_person>
			</survey>
		</writer>
	</field_of_study>
</survey_all>
 
Zuletzt bearbeitet:
Code:
/survey_all//field_of_study[@study_name='Informatik']//@writer_name

so sieht der gelungene XPath Expression aus.


Gruß
m
 
Hi.

Wenn du die getNodeValue() Methode auf einen Element Knoten anwendest (im Gegensatz zu einem Attributknoten), dann ist laut der Tabelle hier: http://java.sun.com/j2se/1.4.2/docs/api/org/w3c/dom/Node.html "null" der erwartete Rückgabewert. Du könntest das Node Objekt in ein Element casten und dann mit der getAttribute(String) Methode auf dein gesuchtes Attribut zuzugreifen.

Allerdings kannst du's auch gleich so machen:
Code:
//field_of_study[@study_name='Informatik']/writer/@writer_name
Gruß
 
Hi,

ich nehme lieber gleich den sauberen XPath Ausdruck, als wieder viel mit casten zu tun haben zu müssen. Ich muss schon genügend casten !

Danke
Gruß
m
 
Zurück