//Using E4X var xmlDoc=new XML(); xmlDoc.load("note.xml"); document.write(xmlDoc.body); //Note: 'body' is actually a tag in note.xml, //but it can be accessed as if it were a regular property of xmlDoc.
/** Loads an XML document from the given input stream. */ protected static Document loadXML(final InputStream in) throws ParserConfigurationException, SAXException, IOException { return createBuilder().parse(in); }
/** Gets the CData beneath the given element's specified child. */ public static String cdata(final Element el, final String child) { NodeList children = el.getElementsByTagName(child); if (children == null || children.getLength() == 0) return null; return cdata(children.item(0)); }
public boolean userExists(final String name) throws IOException { if (!isCapitalized(name)) { throw new IOException( "User name cannot start with a lower-case character: " + name); } final XML xml = query("list", "users", "ususers", name); final NodeList list = xml.xpath("/api/query/users/user"); int count = list.getLength(); for (int i = 0; i < count; i++) { final NamedNodeMap node = list.item(i).getAttributes(); if (node != null && node.getNamedItem("missing") == null) return true; } return false; }
/** Parses XML from the given input stream. */ public XML(final InputStream in) throws ParserConfigurationException, SAXException, IOException { this(null, loadXML(in)); }
/** * Gets the JAR manifest associated with the given XML document. Assumes the * XML document was loaded as a resource from inside a JAR. */ public static Manifest getManifest(final XML xml) throws IOException { final String path = xml.getPath(); if (path == null || !path.startsWith("file:")) return null; final int dotJAR = path.indexOf(".jar!/"); return getManifest(new File(path.substring(5, dotJAR + 4))); }
/** Gets the given element's specified child elements. */ public static ArrayList<Element> elements(final Element el, final String child) { return elements(el.getElementsByTagName(child)); }
@Override public String toString() { try { return dumpXML(doc); } catch (final TransformerException exc) { // NB: Return the exception stack trace as the string. // Although this is a bad idea, I find it somehow hilarious. final ByteArrayOutputStream out = new ByteArrayOutputStream(); exc.printStackTrace(new PrintStream(out)); return out.toString(); } }
/** Gets the CData beneath the given element's specified child. */ public static String cdata(final Element el, final String child) { NodeList children = el.getElementsByTagName(child); if (children == null || children.getLength() == 0) return null; return cdata(children.item(0)); }
public boolean login(final String user, final String password) throws IOException { XML xml = request(null, "login", "lgname", user, "lgpassword", password); final String loginToken = getAttribute(xml.xpath("/api/login"), "token"); if (loginToken == null) { System.err.println("Did not get a token!"); return false; } xml = request(null, "login", "lgname", user, "lgpassword", password, "lgtoken", loginToken); final boolean result = "Success".equals(getAttribute(xml.xpath("/api/login"), "result")); currentUser = result ? user : null; return result; }
/** Parses XML from the given string. */ public XML(final String s) throws ParserConfigurationException, SAXException, IOException { this(null, loadXML(s)); }
/** * Gets the JAR manifest associated with the given XML document. Assumes the * XML document was loaded as a resource from inside a JAR. */ public static Manifest getManifest(final XML xml) throws IOException { final String path = xml.getPath(); if (path == null || !path.startsWith("file:")) return null; final int dotJAR = path.indexOf(".jar!/"); return getManifest(new File(path.substring(5, dotJAR + 4))); }
/** Gets the given element's specified child elements. */ public static ArrayList<Element> elements(final Element el, final String child) { return elements(el.getElementsByTagName(child)); }
@Override public String toString() { try { return dumpXML(doc); } catch (final TransformerException exc) { // NB: Return the exception stack trace as the string. // Although this is a bad idea, I find it somehow hilarious. final ByteArrayOutputStream out = new ByteArrayOutputStream(); exc.printStackTrace(new PrintStream(out)); return out.toString(); } }
public String getPageSource(final String title) throws IOException { final XML xml = query("titles", title, "export", "true", "exportnowrap", "true"); return xml.cdata("/mediawiki/page/revision/text"); }
/ initialized to some XML resembling your example var dataXML = new XML(""); // build XMLList var dataList = dataXML['Data']; // alternative syntax // var dataList = dataXML.Data; // how many data nodes are under a given data node? var length = dataList.length();