2017-03-13 6 views
0

ich eine XML als Java String haben:Attributextraktion XPath

String abc = "<Tags Value = '635' Type = 'Number'/>"; 

Ich versuche, die Value eines solchen XML-Objekt zu extrahieren mit XPath

Was ich jetzt versucht, bis so etwas wie dieses:

InputSource doc = new InputSource(abc); 
XPath xPath = XPathFactory.newInstance().newXPath(); 
XPathExpression expr = xPath.compile("/Tags[@Value]"); 
System.out.println(expr.evaluate(doc)); 

Antwort

1

versuchen es

public class Example { 

    public static void main(String[] args) throws FileNotFoundException, XPathExpressionException { 
     String abc = "<Tags Value = '635' Type = 'Number'/>"; 
     InputSource doc = new InputSource(new StringReader(abc)); 
     XPath xPath = XPathFactory.newInstance().newXPath(); 
     XPathExpression expr = xPath.compile("/Tags/@Value"); 
     System.out.println(expr.evaluate(doc)); // 635 
    } 

} 
1
InputSource doc = new InputSource(abc); 
XPath xPath = XPathFactory.newInstance().newXPath(); 
XPathExpression expr = xPath.compile("/Tags/@Value"); 
System.out.println(expr.evaluate(doc)); 
+0

Ausnahme im Thread "main" java.net.MalformedURLException: kein Protokoll: Betafish

+0

'/ Tags/@ Wert]' haben Sie diese Syntax überprüft ? –

+0

bearbeitet zu rightone – rathna