Main Content

getAttribute

Read HTML attribute of root node of HTML tree

Description

example

str = getAttribute(tree,attr) returns the attribute attr of the root node of tree. If that attribute is not set, then the function returns a missing value.

Examples

collapse all

Read HTML code from the URL /help/textanalytics using webread.

url = "/help/textanalytics";
code = webread(url);

Parse the HTML code using htmlTree.

tree = htmlTree(code);

Find all the hyperlinks in the HTML tree using findElement. The hyperlinks are the nodes with element name "A".

selector = "A";
subtrees = findElement(tree,selector);
subtrees(1:10)
ans = 
  10×1 htmlTree:

    <A class="svg_link navbar-brand" href="/help/textanalytics/ref/s_tid_gn_logo.html"><IMG alt="bat365" class="mw_logo" src="/images/responsive/global/pic-header-bat365-logo.svg"/></A>
    <A class="mwa-nav_login" href="/login?uri=http:/help/textanalytics/index.html">Sign In</A>
    <A href="/products_s_tid_gn_ps.html">Products</A>
    <A href="/solutions_s_tid_gn_sol.html">Solutions</A>
    <A href="/academia_s_tid_gn_acad.html">Academia</A>
    <A href="/support.html?s_tid=gn_supp">Support</A>
    <A href="/matlabcentral/s_tid_gn_mlc.html">Community</A>
    <A href="/company/events_s_tid_gn_ev.html">Events</A>
    <A href="/company/aboutus/contact_us.html?s_tid=gn_cntus">Contact Us</A>
    <A href="/store?s_cid=store_top_nav/and/amp;s_tid=gn_store">How to Buy</A>

Get the hyperlink references using getAttribute. Specify the attribute name "href".

attr = "href";
str = getAttribute(subtrees,attr);
str(1:10)
ans = 10×1 string array
    "/help/textanalytics/ref/s_tid_gn_logo.html"
    "/login?uri=http:/help/textanalytics/index.html"
    "/products_s_tid_gn_ps.html"
    "/solutions_s_tid_gn_sol.html"
    "/academia_s_tid_gn_acad.html"
    "/support.html?s_tid=gn_supp"
    "/matlabcentral/s_tid_gn_mlc.html"
    "/company/events_s_tid_gn_ev.html"
    "/company/aboutus/contact_us.html?s_tid=gn_cntus"
    "/store?s_cid=store_top_nav&s_tid=gn_store"

Input Arguments

collapse all

HTML tree, specified as an htmlTree array.

Attribute name, specified as a string scalar, character vector, or a scalar cell array containing a character vector.

Output Arguments

collapse all

HTML attribute, returned as a string array

More About

collapse all

HTML Elements

A typical HTML element contains the following components:

  • Element name – Name of the HTML tag. The element name corresponds to the Name property of the HTML tree.

  • Attributes – Additional information about the tag. HTML attributes have the form name="value", where name and value denote the attribute name and value respectively. The attributes appear inside the opening HTML tag. To get the attribute values from an HTML tree, use getAttribute.

  • Content – Element content. The content appears between opening and closing HTML tags. The content can be text data or nested HTML elements. To extract the text from an htmlTree object, use extractHTMLText. To get the nested HTML elements of an htmlTree object, use the Children property.

For example, the HTML element <a href="Javascript: void(0)">Home</a> comprises the following components:

ComponentValueDescription
Element nameaElement is a hyperlink
AttributeAttribute namehrefHyperlink reference
Attribute value""Hyperlink reference value
ContentHomeText to display

Version History

Introduced in R2018b