Jan 20
Wie kann man möglichst schnell und ohne viel Aufwand einen XML-Parser in Java, mit den standard Libraries schreiben? Ganz einfach…
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
// [...]
function parseXML(File file) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(file);
NodeList nodeList = doc.getDocumentElement().getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node instanceof Element) {
Element element = (Element) node;
String tagName = element.getTagName();
if ("info".equalsIgnoreCase(tagName)) doSomething(element);
}
}
getSpriteImages(sp, file);
} catch (Exception e) {
e.printStackTrace();
}
}
Recent Comments