# Bestimmte XML-Inhalte in String lesen



## Prod3x (15. Okt 2012)

Guten Morgen 

ich habe ein Problem. Und zwar sollen aus dem XML Inhalt bestimmte Sachen herausgeholt werden und in Strings gespeichert. 
Die XML Datei ist über 80.000 Zeilen lang ist also nur ein Ausschnitt. Benötigt werden aus dem Abschnitt <KeyFamilies> die Strings bei conceptRef:
"FREQUENZY", "TABLE", "COUNTRY", etc.

[XML] <KeyFamilies>
    <structure:KeyFamily id="AIR_A1" agencyID="ESTAT" version="2.5" urn="urn:estat:sdmx.infomodel.keyfamily.KeyFamily=ESTAT:AIR_A1:2.2" isFinal="true">
      <structure:Name xml:lang="en">Flight Stage Table Structure</structure:Name>
      <structure:Components>
        <structureimension conceptRef="FREQUENCY" conceptSchemeRef="CS_AVIATION" conceptSchemeVersion="2.4" conceptSchemeAgency="ESTAT" codelist="CL_FREQ" codelistVersion="1.1" codelistAgency="ESTAT" isFrequencyDimension="true" />
        <structureimension conceptRef="TABLE" conceptSchemeRef="CS_AVIATION" conceptSchemeVersion="2.4" conceptSchemeAgency="ESTAT" codelist="CL_AIR_TABLE" codelistVersion="2.2" codelistAgency="ESTAT">
          <structure:TextFormat textType="String" maxLength="2" />
        </structureimension>
        <structureimension conceptRef="COUNTRY" conceptSchemeRef="CS_AVIATION" conceptSchemeVersion="2.4" conceptSchemeAgency="ESTAT" codelist="CL_AIR_COUNTRY" codelistVersion="2.3" codelistAgency="ESTAT">
          <structure:TextFormat textType="String" maxLength="2" />
        </structureimension>
        <structureimension conceptRef="AIRPORT" conceptSchemeRef="CS_AVIATION" conceptSchemeVersion="2.4" conceptSchemeAgency="ESTAT" codelist="CL_AIR_AIRPORT" codelistVersion="2.2" codelistAgency="ESTAT">
          <structure:TextFormat textType="String" maxLength="4" />
        </structureimension>
        <structureimension conceptRef="PART_AIRPORT" conceptSchemeRef="CS_AVIATION" conceptSchemeVersion="2.4" conceptSchemeAgency="ESTAT" codelist="CL_AIR_AIRPORT" codelistVersion="2.2" codelistAgency="ESTAT">
          <structure:TextFormat textType="String" maxLength="4" />
        </structureimension>
        <structureimension conceptRef="DIRECTION" conceptSchemeRef="CS_AVIATION" conceptSchemeVersion="2.4" conceptSchemeAgency="ESTAT" codelist="CL_AIR_DIRECTION" codelistVersion="2.2" codelistAgency="ESTAT">
          <structure:TextFormat textType="Integer" maxLength="1" />
        </structureimension>
        <structureimension conceptRef="SCHEDULED" conceptSchemeRef="CS_AVIATION" conceptSchemeVersion="2.4" conceptSchemeAgency="ESTAT" codelist="CL_AIR_SCHEDULED" codelistVersion="2.2" codelistAgency="ESTAT">
          <structure:TextFormat textType="Integer" maxLength="1" />
        </structureimension>
        <structureimension conceptRef="SERVICE" conceptSchemeRef="CS_AVIATION" conceptSchemeVersion="2.4" conceptSchemeAgency="ESTAT" codelist="CL_AIR_SERVICES" codelistVersion="2.2" codelistAgency="ESTAT">
          <structure:TextFormat textType="Integer" maxLength="1" />
        </structureimension>
        <structureimension conceptRef="AIRLINE_INFO" conceptSchemeRef="CS_AVIATION" conceptSchemeVersion="2.4" conceptSchemeAgency="ESTAT" codelist="CL_AIR_AIRLINE" codelistVersion="2.3" codelistAgency="ESTAT">
          <structure:TextFormat textType="String" maxLength="3" />
        </structureimension>
        <structureimension conceptRef="AIRCRAFT_TYPE" conceptSchemeRef="CS_AVIATION" conceptSchemeVersion="2.4" conceptSchemeAgency="ESTAT" codelist="CL_AIR_AIRCRAFT" codelistVersion="2.2" codelistAgency="ESTAT">
          <structure:TextFormat textType="String" maxLength="4" />
        </structureimension>
        <structure:TimeDimension conceptRef="TIME_PERIOD" conceptSchemeRef="CS_AVIATION" conceptSchemeVersion="2.4" conceptSchemeAgency="ESTAT">
          <structure:TextFormat textType="ObservationalTimePeriod" />
        </structure:TimeDimension>
        <structurerimaryMeasure conceptRef="OBS_VALUE" conceptSchemeRef="CS_AVIATION" conceptSchemeVersion="2.4" conceptSchemeAgency="ESTAT">
          <structure:TextFormat textType="Integer" />
        </structurerimaryMeasure>
        <structure:Attribute conceptRef="FREIGHT_MAIL" conceptSchemeRef="CS_AVIATION" conceptSchemeVersion="2.4" conceptSchemeAgency="ESTAT" attachmentLevel="Observation" assignmentStatus="Mandatory">
          <structure:TextFormat textType="Integer" maxLength="12" />
        </structure:Attribute>
        <structure:Attribute conceptRef="COMMERCIAL" conceptSchemeRef="CS_AVIATION" conceptSchemeVersion="2.4" conceptSchemeAgency="ESTAT" attachmentLevel="Observation" assignmentStatus="Mandatory">
          <structure:TextFormat textType="Integer" maxLength="12" />
        </structure:Attribute>
        <structure:Attribute conceptRef="SEATS_AVAIL" conceptSchemeRef="CS_AVIATION" conceptSchemeVersion="2.4" conceptSchemeAgency="ESTAT" attachmentLevel="Observation" assignmentStatus="Mandatory">
          <structure:TextFormat textType="Integer" maxLength="12" />
        </structure:Attribute>
      </structure:Components>
    </structure:KeyFamily>
  </KeyFamilies>[/XML]

Mit meinem Programm Code habe ich es geschafft genau diesen oben geposteten Abschnitt in die Konsole auszugeben aber ich bin etwas Ratlos wie ich nun genau die gesuchten Wörter herausfiltern kann.


```
try {
			BufferedReader bf = new BufferedReader(new InputStreamReader(
					System.in));
			String xmlFile = "C://Lokaler Datenbereich/JavaProjek/Mapping/AIR_A1+ESTAT+2.5.xml";
			File file = new File(xmlFile);
			if (file.exists()) {
				DocumentBuilderFactory factory = DocumentBuilderFactory
						.newInstance();
				DocumentBuilder builder = factory.newDocumentBuilder();
				Document doc = builder.parse(xmlFile);
				TransformerFactory tranFactory = TransformerFactory
						.newInstance();
				Transformer aTransformer = tranFactory.newTransformer();
				// Get nodes list of all elements
				NodeList list = doc.getElementsByTagName("*");
				boolean helper = false;
				for (int i = 0; i < list.getLength(); i++) {
					// Get element
					Element element = (Element) list.item(i);
					Source src = new DOMSource(element);
					// System.out.println("Node no: " + i + " is "
					// + element.getNodeName());

					if (element.getNodeName() == "KeyFamilies") {

						System.out
								.println("Its corresponding xml representation:");
						Result dest = new StreamResult(System.out);
						aTransformer.transform(src, dest);
						System.out.println("\n");
					}

				}
			} else {
				System.out.println(xmlFile + " (file name) doesn't found!");
			}
		} catch (Exception e) {
			e.getMessage();
		}
```

Ich bin absoluter Neuling im Bereich XML in Java.

Viele Danke schonmal im Vorraus. :toll:


----------



## SlaterB (15. Okt 2012)

Strings mit equals vergleichen, nicht mit == !

bei so überschaubarer Struktur geht es vielleicht auch ohne getElementsByTagName(),
falls sie feststeht dann hangle dich von Document aus Ebene um Ebene durch,

wie auch immer scheint das StreamResult im if übertrieben, zumal du ja Strings brauchst?
schau dir auch bei den Blatt-Knoten die verfügbaren Methoden an, getText(), getAttributes() usw.,


----------



## Prod3x (15. Okt 2012)

Ich hab es jetzt mit getAttribute() gelöst. Bin mir jetzt nur nicht so sicher ob das eine "elegante" Lösung ist  



```
try {
			BufferedReader bf = new BufferedReader(new InputStreamReader(
					System.in));
			String xmlFile = "AIR_A1+ESTAT+2.5.xml";
			File file = new File(xmlFile);
			if (file.exists()) {
				DocumentBuilderFactory factory = DocumentBuilderFactory
						.newInstance();
				DocumentBuilder builder = factory.newDocumentBuilder();
				Document doc = builder.parse(xmlFile);
				// Get nodes list of all elements
				NodeList list = doc.getElementsByTagName("*");
				for (int i = 0; i < list.getLength(); i++) {
					// Get element
					Element element = (Element) list.item(i);
					String attrValue = element.getAttribute("conceptRef");
					System.out.println(attrValue);

				}
			} else {
				System.out.println(xmlFile + " (file name) doesn't found!");
			}
		} catch (Exception e) {
			e.getMessage();
		}
```


----------

