View Full Version : [Frage] XML Problem
hat jemand schon einen funktionierenden parser??
das beispiel von der folie kann man mal komplett vergessen weil es nicht funkt....
kann da wer helfen?
ich brauch nur ein bsp wie ich ein node zb übergeben muss...
btw: die vorlesung versteht man nicht mal gscheit wenn man auf volle lautstärke aufdrehen...
habs grad heraus gefunden:
code für einfache ausgabe eines XML:
import javax.xml.parsers.*;
import org.xml.sax.InputSource;
import java.io.FileReader;
import java.io.IOException;
import org.w3c.dom.*;
public class dom {
public static void myParser(){
Document document = null;
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new FileReader("C:\\XML\\sample.xml"));
document = builder.parse(is);
echo(document.getFirstChild());
}
catch (Exception e){
System.out.println("Fehler: " + e);
}
}
public static void printlnCommon(Node n){
System.out.print(" nodeName=\"" + n.getNodeName() + "\"");
String val = n.getNodeValue();
if(val != null) { System.out.print(" nodeValue =");
if(val.trim().equals("")) {System.out.print("[WS]");
}else {System.out.print("\"" + n.getNodeValue() + "\"");}} System.out.println();
}
public static void echo(Node n){
int type = n.getNodeType();
switch (type){
case Node.ATTRIBUTE_NODE: System.out.print("ATTR:"); printlnCommon(n); break;
case Node.DOCUMENT_NODE: System.out.print("DOC:"); printlnCommon(n); break;
case Node.ELEMENT_NODE: System.out.print("ELEM:"); printlnCommon(n);
NamedNodeMap atts = n.getAttributes();
for(int i = 0; i < atts.getLength(); i++) { Node att = atts.item(i); echo(att); }
break;
case Node.TEXT_NODE: System.out.print("CHAR:"); printlnCommon(n); break;
default: System.out.print("UNSUPPORTED NODE:" + type); printlnCommon(n); break;}
for (Node child = n.getFirstChild(); child != null; child = child.getNextSibling())
{echo(child);}
}
}
XML sieht zb so aus:
<Parameter>
<File>1</File>
<RTP>2</RTP>
<Capture>3</Capture>
</Parameter>
vBulletin® v3.7.1, Copyright ©2000-2008, Jelsoft Enterprises Ltd.