Skip to content

Instantly share code, notes, and snippets.

@blakehurlburt
Created July 23, 2021 01:57
Show Gist options
  • Save blakehurlburt/7f618f2ac1680952b5bb46c6a4412662 to your computer and use it in GitHub Desktop.
Save blakehurlburt/7f618f2ac1680952b5bb46c6a4412662 to your computer and use it in GitHub Desktop.
TTML XPath example
public final class Main {
public static void main(String[] args) throws Exception {
XPath xpath = XPathFactory.newInstance().newXPath();
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = parser.parse(new InputSource(new FileInputStream("ReplaceWithYourTtmlFile.ttml")));
Node metadata = (Node) xpath.compile("/tt/head/metadata").evaluate(document, XPathConstants.NODE);
System.out.println(nodeToString(metadata));
}
private static String nodeToString(Node node) throws Exception {
StringWriter sw = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(new DOMSource(node), new StreamResult(sw));
return sw.toString();
}
private Main() {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment