2012-12-04 17 views
14

Chciałbym uzyskać wszystkie treści między tagami, ale nie wiem jak to zrobić z powodu urn: namespace.XPath z przestrzenią nazw w Javie

<urn:ResponseStatus version="1.0" xmlns:urn="urn:camera-org"> 

<urn:requestURL>/CAMERA/Streaming/status</urn:requestURL> 
<urn:statusCode>4</urn:statusCode> 
<urn:statusString>Invalid Operation</urn:statusString> 
<urn:id>0</urn:id> 

</urn:ResponseStatus> 

Wszelkie pomysły?

Dzięki!

+1

Jakie biblioteki używasz? Czy próbowałeś poprzedzić zapytanie hasłem "urn:"? –

+0

Używanie javax.xml.xpath.XPath; Próbowałem z urną: ale to nie pomogło. – Sergiu

Odpowiedz

25
  1. Krótka odpowiedź: użyj XPath local-name(). W ten sposób: xPathFactory.newXPath().compile("//*[local-name()='requestURL']/text()"); zwróci /CAMERA/Streaming/status
  2. Lub możesz zaimplementować NamespaceContext, który mapuje nazwy przestrzeni nazw i identyfikatory URI i ustawia je na obiekcie XPath przed wysłaniem zapytania.
  3. Spójrz na tym blog article, Aktualizacja: artykuł jest w dół, można zobaczyć go na webarchive

Rozwiązanie 1 próbkę:

XPath xpath = XPathFactory.newInstance().newXPath(); 
String responseStatus = xpath.evaluate("//*[local-name()='ResponseStatus']/text()", document); 
System.out.println("-> " + responseStatus); 

Rozwiązanie 2 próbki:

// load the Document 
Document document = ...; 
NamespaceContext ctx = new NamespaceContext() { 
    public String getNamespaceURI(String prefix) { 
     return prefix.equals("urn") ? "urn:camera-org" : null; 
    } 
    public Iterator getPrefixes(String val) { 
     return null; 
    } 
    public String getPrefix(String uri) { 
     return null; 
    } 
}; 
XPath xpath = XPathFactory.newInstance().newXPath(); 
xpath.setNamespaceContext(ctx); 
String responseStatus = xpath.evaluate("//urn:ResponseStatus/text()", document); 
System.out.println("-> " + responseStatus); 

Edytuj

Jest to kompletny przykład, to poprawnie pobrać element:

String xml = "<urn:ResponseStatus version=\"1.0\" xmlns:urn=\"urn:camera-org\">\r\n" + // 
     "\r\n" + // 
     "<urn:requestURL>/CAMERA/Streaming/status</urn:requestURL>\r\n" + // 
     "<urn:statusCode>4</urn:statusCode>\r\n" + // 
     "<urn:statusString>Invalid Operation</urn:statusString>\r\n" + // 
     "<urn:id>0</urn:id>\r\n" + // 
     "\r\n" + // 
     "</urn:ResponseStatus>"; 
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
factory.setNamespaceAware(true); 
DocumentBuilder builder = factory.newDocumentBuilder(); 
Document doc = builder.parse(new java.io.ByteArrayInputStream(xml.getBytes())); 
XPath xpath = XPathFactory.newInstance().newXPath(); 
xpath.setNamespaceContext(new NamespaceContext() { 
    public String getNamespaceURI(String prefix) { 
     return prefix.equals("urn") ? "urn:camera-org" : null; 
    } 

    public Iterator<?> getPrefixes(String val) { 
     return null; 
    } 

    public String getPrefix(String uri) { 
     return null; 
    } 
}); 
XPathExpression expr = xpath.compile("//urn:ResponseStatus"); 
Object result = expr.evaluate(doc, XPathConstants.NODESET); 
NodeList nodes = (NodeList) result; 
for (int i = 0; i < nodes.getLength(); i++) { 
    Node currentItem = nodes.item(i); 
    System.out.println("found node -> " + currentItem.getLocalName() + " (namespace: " + currentItem.getNamespaceURI() + ")"); 
} 
+0

działa świetnie! Wielkie dzięki ! – Sergiu

+1

Dobra odpowiedź! (Ten wpis na blogu byłby bardziej przydatny, gdyby kod był wcięty i przykładowy XML nie został zjedzony przez jego własny serwer.) No cóż, nadal przydatna lista na końcu.) –

+1

Dzięki za tonę !! Przetworzyłem olbrzymi złożony plik xml przy użyciu tej logiki i działało to dla mnie. –

0

XML xpath z Namespace

Proste XML

String namespaceXML = "<?xml version='1.0' ?><information><person id='1'><name>Deep</name><age>34</age><gender>Male</gender></person> <person id='2'><name>Kumar</name><age>24</age><gender>Male</gender></person> <person id='3'><name>Deepali</name><age>19</age><gender>Female</gender></person><!-- more persons... --></information>"; 
String jsonString = "{}"; 
String expression = "//information"; 

przestrzeni nazw XML

String namespaceXML = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><m:NumberToDollarsResponse xmlns:m=\"http://www.dataaccess.com/webservicesserver/\"><m:NumberToDollarsResult>nine hundred and ninety nine dollars</m:NumberToDollarsResult></m:NumberToDollarsResponse></soap:Body></soap:Envelope>"; 
String jsonString = "{'soap':'http://schemas.xmlsoap.org/soap/envelope/', 'm':'http://www.dataaccess.com/webservicesserver/'}"; 
String expression = "//m:NumberToDollarsResponse/m:NumberToDollarsResult/text()"; 

Supply plik nazw XML jako ciąg, aby asscerionXpath(namespaceXML, jsonString, expression) metody i uzyskać wynik w postaci tekstu/węzła.

text(): nine hundred and ninety nine dollars

węzeł: <m:NumberToDollarsResult xmlns:m="http://www.dataaccess.com/webservicesserver/"> nine hundred and ninety nine dollars </m:NumberToDollarsResult>

public static String asscerionXpath(String namespaceXML, String jsonString, String expression){ 
    if(namespaceXML.indexOf("><") > -1) namespaceXML = namespaceXML.replace("><", ">\r\n<"); 
    if(jsonString.indexOf("'") > -1) jsonString = jsonString.replace("'", "\""); 

    System.out.println("namespaceXML : \n"+namespaceXML); 
    System.out.println("nsmespaces : \n"+jsonString); 

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
    factory.setValidating(false); 
    factory.setNamespaceAware(true); 
    factory.setIgnoringComments(true); 
    factory.setIgnoringElementContentWhitespace(true); 
    try { 
     DocumentBuilder builder = factory.newDocumentBuilder(); 
     Document source = builder.parse(string2Source(namespaceXML)); 
     XPath xpath = XPathFactory.newInstance().newXPath(); 

     addNameSpaces(jsonString, xpath); 
     // An XPath expression is not thread-safe. Make sure it is accessible by only one Thread. 
     XPathExpression expr = xpath.compile(expression); 

     // The NodeList interface provides the abstraction of an ordered collection of nodes, 
     NodeList nodes = (org.w3c.dom.NodeList) expr.evaluate(source, XPathConstants.NODESET);; 
     Node tree_base = nodes.item(0); 
     return document2String(tree_base); 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (SAXException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (ParserConfigurationException e) { 
     e.printStackTrace(); 
    } catch (XPathExpressionException e) { 
     System.out.println("If the expression cannot be evaluated."); 
    } 
    return ""; 
} 
static InputSource string2Source(String str) { 
    InputSource inputSource = new InputSource(new StringReader(str)); 
    return inputSource; 
} 
static void addNameSpaces(String jsonString, XPath xpath) { 
    JSONParser parser = new JSONParser(); 
    try { 
     JSONObject namespaces = (JSONObject) parser.parse(jsonString); 

     if (namespaces.size() > 0) { 
      final JSONObject declaredPrefix = namespaces; // To access in Inner-class. 
      NamespaceContext nameSpace = new NamespaceContext() { 
       // To get all prefixes bound to a Namespace URI in the current scope, XPath 1.0 specification 
       // --> "no prefix means no namespace" 
       public String getNamespaceURI(String prefix) { 
        Iterator<?> key = declaredPrefix.keySet().iterator(); 
        System.out.println("Keys : "+key.toString()); 
        while (key.hasNext()) { 
         String name = key.next().toString(); 
         if (prefix.equals(name)) { 
          System.out.println(declaredPrefix.get(name)); 
          return declaredPrefix.get(name).toString(); 
         } 
        } 
        return ""; 
       } 
       public Iterator<?> getPrefixes(String val) { 
        return null; 
       } 
       public String getPrefix(String uri) { 
        return null; 
       } 
      };// Inner class. 

      xpath.setNamespaceContext(nameSpace); 
     } 

    } catch (org.json.simple.parser.ParseException e) { 
     e.printStackTrace(); 
    } 
} 
Powiązane problemy