Skip to content

Instantly share code, notes, and snippets.

@gastaldi
Last active March 22, 2016 19:13
Show Gist options
  • Save gastaldi/d8a81219cdbc91ef1f7c to your computer and use it in GitHub Desktop.
Save gastaldi/d8a81219cdbc91ef1f7c to your computer and use it in GitHub Desktop.
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.jboss.forge.service.main;
import java.io.InputStream;
import java.io.StringReader;
import java.net.URL;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import org.xml.sax.InputSource;
/**
*
* @author <a href="mailto:[email protected]">George Gastaldi</a>
*/
public class XPathExample
{
public static void main(String[] args) throws Exception
{
XPath xpath = XPathFactory.newInstance().newXPath();
String data = readURL();
System.out.println(
xpath.evaluate("metadata/versioning/snapshot/timestamp", new InputSource(new StringReader(data))));
System.out.println(
xpath.evaluate("metadata/versioning/snapshot/buildNumber", new InputSource(new StringReader(data))));
}
private static String readURL() throws Exception
{
URL url = new URL(
"http://repository-projectodd.forge.cloudbees.com/snapshot/org/wildfly/swarm/jaxrs-runtime/1.0.0.Beta5-SNAPSHOT/maven-metadata.xml");
try (InputStream is = url.openStream())
{
byte[] contents = new byte[is.available()];
is.read(contents);
return new String(contents);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment