# Attribute aus XML Auslesen



## dior (11. Aug 2015)

Hallo,

ich habe eine XML in folgendem Format:


```
<?xml version="1.0" encoding="utf-8"?>
<RealTimeNetworkStatus FormatVersion="1.1" Generator="TestGenerator">
  <Date>2015-08-07</Date>
  <Time>12:06:00.0</Time>
  <ElevationMask>10.0</ElevationMask>
  <NetworkClusters>
    <Cluster Name="Cluster1">
      <SatelliteSystems>
        <System Name="GPS">
          <Site Name="Kls1" visible="4" visible2="0" visible3="8"/>
          <Site Name="Trz1" visible="8" visible2="8" visible3="6"/>
          <Site Name="Blo1" visible="8" visible2="8" visible3="8"/>
          <Site Name="Lea1" visible="8" visible2="8" visible3="3"/>
          <Site Name="Lad1" visible="8" visible2="8" visible3="7"/>
          <Site Name="Lia1" visible="8" visible2="8" visible3="8"/>
          <Site Name="Kos1" visible="8" visible2="7" visible3="6"/>
          <Site Name="Sxc1" visible="8" visible2="8" visible3="5"/>
        </System>
        <System Name="Cluster2">
          <Site Name="Kls1" visible="7" visible2="7" visible3="6"/>
          <Site Name="Trz1" visible="7" visible2="7" visible3="3"/>
          <Site Name="Blo1" visible="7" visible2="7" visible3="6"/>
          <Site Name="Lea1" visible="7" visible2="7" visible3="5"/>
          <Site Name="Lad1" visible="7" visible2="7" visible3="6"/>
          <Site Name="Lia1" visible="7" visible2="7" visible3="6"/>
          <Site Name="Kos1" visible="7" visible2="7" visible3="5"/>
          <Site Name="Sxc1" visible="7" visible2="7" visible3="4"/>
        </System>
      </SatelliteSystems>
    </Cluster>
  </NetworkClusters>
</RealTimeNetworkStatus>
```

ich schaffe es aber nicht die Site Attribute auszulesen.
Ich bräuchte am besten jedes Attribut in einer Liste.

Mein bisheriger Versuch sieht so aus:

```
SAXBuilder saxBuilder = new SAXBuilder();

  Document document = saxBuilder.build(inputFile);
  System.out.println("Root element :"
  + document.getRootElement().getName());

  Element classElement = document.getRootElement();

  List<Element> Cluster = classElement.getChildren();
  List<Attribute> Cluster2 = classElement.getAttributes();
  System.out.println(Cluster.toString());
  System.out.println(Cluster2.toString());
  System.out.println("----------------------------");

  Document doc = (Document) saxBuilder.build(xmlFile);

  List stations = doc.getRootElement().getChildren("Site");
  for (int i = 1; i <= stations.size(); i++) {
  Element stationen = (Element) stations.get(i);
  String name = stationen.getAttributeValue("Name");
  System.out.println("Site Name: " + name);
  }
```

Es wäre echt nett wenn mir hierbei jemand helfen könnte!

danke


----------



## Flown (11. Aug 2015)

Du verwendest JDOM sehe ich das richtig?

Würde es dir auch mit dem normalen DOM-Parser reichen?

```
String s = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"
     + "<RealTimeNetworkStatus FormatVersion=\"1.1\" Generator=\"TestGenerator\">\r\n"
     + "  <Date>2015-08-07</Date>\r\n" + "  <Time>12:06:00.0</Time>\r\n"
     + "  <ElevationMask>10.0</ElevationMask>\r\n" + "  <NetworkClusters>\r\n"
     + "  <Cluster Name=\"Cluster1\">\r\n" + "  <SatelliteSystems>\r\n"
     + "  <System Name=\"GPS\">\r\n"
     + "  <Site Name=\"Kls1\" visible=\"4\" visible2=\"0\" visible3=\"8\"/>\r\n"
     + "  <Site Name=\"Trz1\" visible=\"8\" visible2=\"8\" visible3=\"6\"/>\r\n"
     + "  <Site Name=\"Blo1\" visible=\"8\" visible2=\"8\" visible3=\"8\"/>\r\n"
     + "  <Site Name=\"Lea1\" visible=\"8\" visible2=\"8\" visible3=\"3\"/>\r\n"
     + "  <Site Name=\"Lad1\" visible=\"8\" visible2=\"8\" visible3=\"7\"/>\r\n"
     + "  <Site Name=\"Lia1\" visible=\"8\" visible2=\"8\" visible3=\"8\"/>\r\n"
     + "  <Site Name=\"Kos1\" visible=\"8\" visible2=\"7\" visible3=\"6\"/>\r\n"
     + "  <Site Name=\"Sxc1\" visible=\"8\" visible2=\"8\" visible3=\"5\"/>\r\n"
     + "  </System>\r\n" + "  <System Name=\"Cluster2\">\r\n"
     + "  <Site Name=\"Kls1\" visible=\"7\" visible2=\"7\" visible3=\"6\"/>\r\n"
     + "  <Site Name=\"Trz1\" visible=\"7\" visible2=\"7\" visible3=\"3\"/>\r\n"
     + "  <Site Name=\"Blo1\" visible=\"7\" visible2=\"7\" visible3=\"6\"/>\r\n"
     + "  <Site Name=\"Lea1\" visible=\"7\" visible2=\"7\" visible3=\"5\"/>\r\n"
     + "  <Site Name=\"Lad1\" visible=\"7\" visible2=\"7\" visible3=\"6\"/>\r\n"
     + "  <Site Name=\"Lia1\" visible=\"7\" visible2=\"7\" visible3=\"6\"/>\r\n"
     + "  <Site Name=\"Kos1\" visible=\"7\" visible2=\"7\" visible3=\"5\"/>\r\n"
     + "  <Site Name=\"Sxc1\" visible=\"7\" visible2=\"7\" visible3=\"4\"/>\r\n"
     + "  </System>\r\n" + "  </SatelliteSystems>\r\n" + "  </Cluster>\r\n"
     + "  </NetworkClusters>\r\n" + "</RealTimeNetworkStatus>";

try {
   Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
       .parse(new InputSource(new StringReader(s)));

   doc.getDocumentElement().normalize();
   NodeList sites = doc.getElementsByTagName("Site");
   for (int i = 0; i < sites.getLength(); i++) {
     Node cur = sites.item(i);
     NamedNodeMap attributes = cur.getAttributes();
     Node name = attributes.getNamedItem("Name");
     System.out.println(i + ": " + name.getNodeValue());
   }
} catch (SAXException | IOException | ParserConfigurationException e) {
   e.printStackTrace();
}
```

EDIT: Mit JDOM musst du mit der Funktion `document.getRootElement().getDescendants(...);` suchen:

```
try {
   Document document = new SAXBuilder().build(new InputSource(new StringReader(s)));

   for (Element e : document.getRootElement().getDescendants(new ElementFilter("Site"))) {
     System.out.println(e.getAttributeValue("Name"));
   }
} catch (JDOMException | IOException e) {
   e.printStackTrace();
}
```


----------



## dior (11. Aug 2015)

Hallo Flown,

vielen Dank.... das hat Funktioniert!

Noch eine Frage, kann ich so auch einfach auf einzelne Elemente zugreifen? Also nicht alle Auflisten sondern z.B. Zeig mir visible3 bei Name = Kls1 / Cluster1.


----------



## dior (11. Aug 2015)

Ich hab das jetzt einfach so gemacht (keine Ahnung ob es der beste Weg oder komplett umständlich ist...):


```
List<Pair> mPairs = new ArrayList<Pair>();
   Pair pair;
    for (Element e : document.getRootElement().getDescendants(new ElementFilter("Site"))) {
      System.out.println("Name: " + e.getAttributeValue("Name") + " - Visible: " + e.getAttributeValue("visible"));
      //System.out.println("aaaaa: " + e.getValue());
      pair = new Pair(e.getAttributeValue("Name"), e.getAttributeValue("visible"));
      mPairs.add(pair);
    }
   
    System.out.println(mPairs.get(3));
```


----------



## Flown (11. Aug 2015)

Du kannst mit XPath arbeiten, dann sieht die Query ca. so aus:

```
List<Attribute> evaluation = XPathFactory.instance()
     .compile("//Cluster[@Name='Cluster1']//Site[@Name='Kls1']/@visible3", new AttributeFilter())
     .evaluate(document);
for (Attribute a : evaluation) {
   System.out.println(a);
}
```

Dazu brauchst du eine extra Library die: jaxen.jar


----------



## dior (11. Aug 2015)

Danke!!


----------

