Extract Data and Resources from HTML

Aspose.HTML for Java loads HTML from a file, URL, string, or stream into an HTMLDocument. Java applications can inspect the DOM, select elements, read text and attributes, resolve relative URLs, and save the required values or linked resources.

Use DOM methods, CSS selectors, or XPath expressions when the required information is part of the document structure. When an element points to an image, SVG file, stylesheet, or another resource, use the resolved URL and the network service to download its bytes. This separates HTML analysis from resource retrieval and keeps the extraction logic focused on the data your application needs.


Choose a Java Data Extraction Task

Select a focused example for the type of content or resource you need to collect.

Extract Website Images

Find image elements, resolve relative source URLs, and save the referenced image files.

Extract Website SVG

Save inline SVG markup and download external SVG resources referenced by an HTML page.

Download File from URL

Request a known resource URL, check the response, and write the returned bytes to a local file.


Extract Links from HTML in Java

The example loads an HTML page, reads every anchor href, resolves relative references against the document URL, and writes distinct HTTP and HTTPS links to output/links.txt. The same selection pattern can be adapted to text, metadata, form values, or resource attributes.

Extract Links from HTML in Java

Path outputPath = Paths.get("output", "links.txt");
Files.createDirectories(outputPath.getParent());

try (HTMLDocument document = new HTMLDocument("https://docs.aspose.com/html/java/")) {
    HTMLCollection anchors = document.getElementsByTagName("a");
    Set<String> links = new LinkedHashSet<>();
    Iterator<Element> iterator = anchors.iterator();

    while (iterator.hasNext()) {
        Element anchor = iterator.next();
        String href = anchor.getAttribute("href");
        if (href.isEmpty()) {
            continue;
        }

        Url linkUrl = new Url(href, document.getBaseURI());
        String protocol = linkUrl.getProtocol();
        if ("http:".equals(protocol) || "https:".equals(protocol)) {
            links.add(linkUrl.getHref());
        }
    }

    Files.write(outputPath, links, StandardCharsets.UTF_8);
}

Steps to Extract Links from HTML

  1. Load the source with the HTMLDocument class.
  2. Select anchor elements with getElementsByTagName("a").
  3. Read each href attribute and ignore empty values.
  4. Resolve relative references with the Url class and the document base URI.
  5. Keep HTTP and HTTPS links, remove duplicates, and save the collection to a text file.

Find HTML Data with DOM, CSS Selectors, or XPath

Choose the selection method that matches the structure of the source document:

  • DOM collections – use getElementsByTagName() when every element with a known tag is required.
  • CSS selectors – use querySelector() for the first match or querySelectorAll() for all elements matching a CSS selector.
  • XPath – use XPath expressions when selection depends on hierarchy, attributes, or text conditions that are awkward to express with a CSS selector.
  • Resource URLs – read attributes such as src or href, resolve them against the document base URI, and request only the files the application needs.

See HTML Navigation for DOM, CSS selector, and XPath examples, or open the Data Extraction documentation for resource-focused examples.


HTML Data Extraction FAQ

1. Can Aspose.HTML for Java extract selected data without saving the complete web page?

Yes. Load the source into an HTMLDocument, select only the required nodes, and store their text, attributes, or referenced resources. The application does not need to save a complete copy of the page.

2. How can I extract only specific HTML elements in Java?

Use getElementsByTagName() for a known tag, querySelector() or querySelectorAll() for CSS-based matching, or XPath when the selection depends on document hierarchy, attributes, or text conditions.

3. Can Aspose.HTML for Java extract table rows and cells from HTML?

Yes. Select the table, tr, th, and td elements through the DOM, then map their text and attributes to the data structure required by the Java application.

4. Is Aspose.HTML for Java free for data extraction?

Aspose.HTML for Java is a commercial library. You can download an evaluation version and request a temporary license to test data extraction without evaluation limitations before purchasing.


Install Aspose.HTML for Java

Add Aspose.HTML for Java from the official Aspose repository. Replace ASPOSE_HTML_VERSION with an exact version from the Aspose.HTML for Java releases page, then reload or rebuild the project so the dependency is resolved.

Add the repository and dependency to the Maven project:

Maven – pom.xml

<repositories>
    <repository>
        <id>AsposeJavaAPI</id>
        <url>https://releases.aspose.com/java/repo/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-html</artifactId>
        <version>ASPOSE_HTML_VERSION</version>
    </dependency>
</dependencies>

Add the repository and dependency to the Gradle project:

Gradle – build.gradle

repositories {
    maven {
        url = uri('https://releases.aspose.com/java/repo/')
    }
}
dependencies {
    implementation 'com.aspose:aspose-html:ASPOSE_HTML_VERSION'
}

Use a JDK compatible with the selected release. For manual JAR installation, Kotlin DSL, updates, and dependency troubleshooting, see Install Aspose.HTML for Java .

Related Aspose.HTML for Java Tasks

Continue with focused Java guides for parsing, creating, converting, and merging HTML documents.