ERLEDIGT
NEIN
NEIN
ANTWORTEN
5
5
ZUGRIFFE
5928
5928
EMPFEHLEN
-
10.05.06 09:23 #1
- Registriert seit
- Apr 2006
- Beiträge
- 9
Hi,
Ich hab da ein kleines Problem... ich bekomme den XPath (javax.xml.xpath.*) obwohl das schon etliche geschafft haben...
bei mir liefert er immer nur ein NULL zurück... es sei denn ich gebe im xpath kein element sondern ein attribut an.. dann bekomm ich das attribut... das kann doch nicht sein?
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
org.apache.html.dom.HTMLDocumentImpl: #document = null org.apache.xerces.dom.DocumentTypeImpl: HTML = null org.apache.xerces.dom.ElementNSImpl: HTML = null org.apache.xerces.dom.TextImpl: #text = org.apache.xerces.dom.ElementNSImpl: HEAD = null org.apache.xerces.dom.TextImpl: #text = org.apache.xerces.dom.ElementNSImpl: TITLE = null org.apache.xerces.dom.TextImpl: #text = Ich bin dieueberschrift org.apache.xerces.dom.TextImpl: #text = org.apache.xerces.dom.ElementNSImpl: META = null org.apache.xerces.dom.TextImpl: #text = org.apache.xerces.dom.ElementNSImpl: LINK = null org.apache.xerces.dom.TextImpl: #text = org.apache.xerces.dom.TextImpl: #text = org.apache.xerces.dom.ElementNSImpl: BODY = null org.apache.xerces.dom.TextImpl: #text = org.apache.xerces.dom.ElementNSImpl: DIV = null org.apache.xerces.dom.TextImpl: #text = org.apache.xerces.dom.ElementNSImpl: A = null org.apache.xerces.dom.TextImpl: #text = org.apache.xerces.dom.ElementNSImpl: IMG = null org.apache.xerces.dom.TextImpl: #text = org.apache.xerces.dom.TextImpl: #text = org.apache.xerces.dom.ElementNSImpl: BR = null org.apache.xerces.dom.TextImpl: #text = org.apache.xerces.dom.ElementNSImpl: BR = null org.apache.xerces.dom.TextImpl: #text = org.apache.xerces.dom.ElementNSImpl: BR = null org.apache.xerces.dom.TextImpl: #text = quote: org.apache.xerces.dom.ElementNSImpl: BR = null org.apache.xerces.dom.TextImpl: #text = The whole notion of passwords is based on an oxymoron. The idea is to have a random string that is easy to remember. Unfortunately, if it's easy to remember, it's something nonrandom like 'Susan.' And if it's random, like 'r7U2*Qnp', it's not easy. org.apache.xerces.dom.TextImpl: #text =
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
public static Node getNodeformDOMXPath(Node parent, String sXPath) { Node node = null; try { // Create a new XPath factory XPathFactory factory = XPathFactory.newInstance(); // Create a new XPath instance XPath xpath = factory.newXPath(); XPathExpression xexpr = xpath.compile(sXPath); node = (Node)xexpr.evaluate(parent, XPathConstants.NODE); } catch (XPathExpressionException e) { e.printStackTrace(); } return node; }
Code :1 2 3 4 5 6 7 8 9 10 11
// GEHT NICHT // String s = "//HTML/HEAD/TITLE"; // GEHT String s = "//@height"; Node node = getNodeformDOMXPath(domParser.getDocument(), s); if (node != null) { System.out.println(node); } else { System.out.println("NODE = NULL"); }
-
10.05.06 10:02 #2
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.886
- Blog-Einträge
- 29
Hallo!
Schau mal hier:
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
/** * */ package de.tutorials; import java.io.StringReader; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Text; import org.xml.sax.InputSource; /** * @author Tom * */ public class XPathExample { /** * @param args */ public static void main(String[] args) { String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; xml += "<html>"; xml += "<head>"; xml += "<title> Strict DTD XHTML Example </title>"; xml += "</head>"; xml += "<body>"; xml += "<p>"; xml += "Please Choose a Day:"; xml += "<br /><br />"; xml += "<select name=\"day\">"; xml += "<option selected=\"selected\">Monday</option>"; xml += "<option>Tuesday</option>"; xml += "<option>Wednesday</option>"; xml += "</select>"; xml += "</p>"; xml += "</body>"; xml += "</html>"; System.out.println(xml); try { Object o = XPathFactory.newInstance().newXPath().evaluate( "/html/body/p/select/option[@selected='selected']/text()", new InputSource(new StringReader(xml)), XPathConstants.NODE); System.out.println(o); } catch (XPathExpressionException e) { e.printStackTrace(); } } }
Gruss TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
10.05.06 10:16 #3
- Registriert seit
- Apr 2006
- Beiträge
- 9
hi,
schonmal schönen dank,
soweit klappt es ja auch das ist nicht das problem...
wenn ich jetzt XPath "/html/body/p/text()" mache erhalte ich ja den text... das war bis jetzt auch kein problem...
allerdings will ich halt XPath "/html/body/p" machen und damit das Node "P" zurückbekommen mit allen childNodes (sprich childNode: br, select, option...)
wenn ich aber XPath "/html/body/p" mache erhate ich wieder nur ein NULL
verstehst hoffe ich was ich brauche... halt als return ein Node mit semtlichen ChildNodes... und wie bereits erwähnt bekomme ich nur ein NULL
-
10.05.06 10:26 #4
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.886
- Blog-Einträge
- 29
Hallo!
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
/** * */ package de.tutorials; import java.io.StringReader; import java.util.Arrays; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; /** * @author Tom * */ public class XPathExample { /** * @param args */ public static void main(String[] args) { String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; xml += "<html>"; xml += "<head>"; xml += "<title> Strict DTD XHTML Example </title>"; xml += "</head>"; xml += "<body>"; xml += "<p>"; xml += "Please Choose a Day:"; xml += "<br /><br />"; xml += "<select name=\"day\">"; xml += "<option selected=\"selected\">Monday</option>"; xml += "<option>Tuesday</option>"; xml += "<option>Wednesday</option>"; xml += "</select>"; xml += "</p>"; xml += "</body>"; xml += "</html>"; System.out.println(xml); try { Object o = XPathFactory.newInstance().newXPath().evaluate( "/html/body/p", new InputSource(new StringReader(xml)), XPathConstants.NODE); Node node = (Node) o; treeWalk(node, 0); } catch (XPathExpressionException e) { e.printStackTrace(); } } private static void treeWalk(Node node, int level) { System.out.print(spaces(level)); System.out.println(node); if (node.hasChildNodes()) { NodeList children = node.getChildNodes(); for (int i = 0, len = children.getLength(); i < len; i++) { treeWalk(children.item(i), level + 1); } } } private static String spaces(int level) { char[] spaces = new char[level]; Arrays.fill(spaces, ' '); return String.valueOf(spaces); } }
Gruss TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
10.05.06 10:50 #5
- Registriert seit
- Apr 2006
- Beiträge
- 9
oh mein gott... tyspisch mittwoch morgen LOL
okay also das schaut soweit schon gut aus... bekomm ja den node *grml*
hab da aber noch immer ein problem
also mein Problem ist jetzt das das XML document nicht als InputSource vorliegt sondern als (Document) geparset vom Xerces->CyberNeko...
wenn ich ihm nämlich nun keinen InputStream übergebe sondern den geparste Neko DOM dann hab ich nämlich genau das problem das ich bei XPath /html/body/p nichts erhalte... was ausschließlich funktioniert ist //@name also Attribut-zugriff
ist extrem merkwürdig
Testumgebung:
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
import java.io.StringReader; import java.io.IOException; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Node; import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.cyberneko.html.parsers.DOMParser; public class Test { public static void main (String args[]) { String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; xml += "<html>"; xml += "<head>"; xml += "<title> Strict DTD XHTML Example </title>"; xml += "</head>"; xml += "<body>"; xml += "<p>"; xml += "Please Choose a Day:"; xml += "<br /><br />"; xml += "<select name=\"day\">"; xml += "<option selected=\"selected\">Monday</option>"; xml += "<option>Tuesday</option>"; xml += "<option>Wednesday</option>"; xml += "</select>"; xml += "</p>"; xml += "</body>"; xml += "</html>"; System.out.println(xml); try { DOMParser domParser = new DOMParser(); domParser.parse(new InputSource(new StringReader(xml))); Document doc = domParser.getDocument(); printDOMTree(doc, ""); Node o = (Node)XPathFactory.newInstance().newXPath().evaluate("/html/head/title/text()", doc, XPathConstants.NODE); System.out.println(o); printDOMTree(o, ""); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (XPathExpressionException e) { e.printStackTrace(); } } public static void printDOMTree(Node node, String sIndent) { System.out.println(sIndent + node.getClass().getName() + ": " + node.getNodeName() + " = " + node.getNodeValue()); Node child = node.getFirstChild(); while (child != null) { printDOMTree(child, sIndent + " "); child = child.getNextSibling(); } } }
Geändert von mad_dark_angel (10.05.06 um 11:06 Uhr)
-
10.05.06 14:39 #6
- Registriert seit
- Apr 2006
- Beiträge
- 9
hmm interessant...
mit "//*[name()='title']/text()" scheint es zu funktionieren... völlig unverständlich... kann mir da jemand helfen? oder hat jemand eine erklärung dafür? weil es muss dann doch auch mit //title/text() funktionieren oder nicht?!
Ähnliche Themen
-
Java XML und Xpath Document speichern
Von duennes im Forum JavaAntworten: 2Letzter Beitrag: 28.11.08, 15:46 -
Html/XML mit XPath auslesen in Java
Von basmati123 im Forum JavaAntworten: 10Letzter Beitrag: 19.05.08, 18:31 -
XPath mit Java und w3c-Document
Von karstenkurt im Forum JavaAntworten: 0Letzter Beitrag: 01.04.08, 09:31 -
Java, XPath und die Frage nach der Elementnummer
Von marbles im Forum JavaAntworten: 1Letzter Beitrag: 22.06.05, 16:27 -
Java DOM: Namespace/XPath Problem
Von kirlew im Forum JavaAntworten: 0Letzter Beitrag: 29.03.05, 15:29





Zitieren

Login





