Get Attribute des Root-Elements im XML unter Verwendung von xpath-Abfrage-Ausdruck in Java

Habe ich diese XML -

     <Results SchemaVersion="1.0" SchemaType="Results" GroupId="-12345" 
     xmlns="http://xyz"  xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
            <Attempt>
                            <Time>2007-03-30T15:58:15</Time>
                            <Message>This is some message</Message>
            </Attempt>
            <Attempt>
                            <Time>2007-03-30T15:59:45</Time>
                            <Message>This is some other message</Message>
            </Attempt>
      </Results>

Und ich habe diesen code in Java, die analysiert, die über xml. Ich möchte die Attribute des root-Elements im xml unter Verwendung von xpath-Abfrage. Ich bin in der Lage, um den Wert des root-Elements, aber nicht die Attribute.
Hinweis: ich weiß nicht, die Namen der attribute, in diesem Fall, sonst hätte ich direkt zugegriffen, diese Attribute

    public class Try {

      public static void main(String args[]){

       try{
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true); 
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse("C:/Documents and Settings/tulans/workspace/WebServiceTool/src/main/resources/Input.xml");
        XPath xpath = XPathFactory.newInstance().newXPath();
        XPathExpression expr = xpath.compile("/*");
        Object result = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;     
        System.out.println(nodes.item(0).getLocalName());
        System.out.println(nodes.item(0).getNodeName());

       }catch(Exception e){
         System.out.println(e);
       }
     }
    }

Bekomme ich Folgende Ergebnisse:

    Results
    Results

Ich will auch root-Elemente Attribut :

     SchemaVersion="1.0" SchemaType="Results" GroupId="-12345" 
     xmlns="http://xyz"  xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
InformationsquelleAutor Sanket | 2012-10-09
Schreibe einen Kommentar