Skip to content

Instantly share code, notes, and snippets.

@jhannes
Created June 26, 2012 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhannes/2998864 to your computer and use it in GitHub Desktop.
Save jhannes/2998864 to your computer and use it in GitHub Desktop.
Syntax proof-of-concept for XML library
public class Demo {
private static Object baseUrl;
public static void main(String[] args) {
Namespace SOAP_NS = new Namespace("http://soap.com", "SOAP");
Namespace MSG_NS = new Namespace("http://msg.com", "msg");
TagName envelopeTag = SOAP_NS.tag("Envelope");
Element envelope = envelopeTag.create(
SOAP_NS.el("Header"),
SOAP_NS.el("Body",
MSG_NS.el("foo",
MSG_NS.el("bar", "fifteen"),
MSG_NS.el("bar", "sixteen").attr("a", "1"),
MSG_NS.el("bar", "seventeen").attr("a", "2"),
MSG_NS.el("a", "Link text").attr(MSG_NS.attribute("href", "http://link")),
MSG_NS.el("div").attr("id", "divId")))
.namespace(MSG_NS));
System.out.println(envelope.toString());
System.out.println(envelope.toFormattedString());
List<String> texts = envelope.find(envelopeTag, SOAP_NS.tag("Body"))
.find(MSG_NS.tag("foo"), MSG_NS.tag("bar")).texts();
System.out.println(texts);
List<String> attrs = envelope.find("Envelope", "SOAP:Body", MSG_NS.tag("foo"), MSG_NS.tag("bar")).attrs("a");
System.out.println(attrs);
envelope.find(envelopeTag, SOAP_NS.tag("Body"))
.find(MSG_NS.tag("foo"), MSG_NS.tag("bar")).get(2).deleteElements();
System.out.println(texts);
envelope.find("Envelope", "Body", SOAP_NS.tag("foo")).checkPath();
Element html = Element.el("div",
Element.textEl("Hello there, what "),
Element.el("em", "is").attr("id", "something"),
Element.textEl(" going on "),
Element.el("a", "here?").attr("href", "http://www.google.com").attr("class", "link"));
System.out.println(html.toFormattedString());
html.find("...", ".something").text("would be");
html.find("...", "#link").attr("foo", "bar");
html.find("...", "a#link").text("there");
html.find("#form", "#first_name").val("Johannes");
html.find("#form", "#last_name").val("Brodwall");
String url = baseUrl + "?" + HtmlSupport.toUrlQueryString(html.find("#form"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment