Use XML to retrieve the character data of an element

Source: Internet
Author: User
In the script program listed in 9-3, the text attribute of each sub-element (TITLE, AUTHOR, BINDING, PAGES, and PRICE) is used as a shortcut to get element character data. For example, the following program code is used to retrieve character data in the TITLE element:

The script program in Table 9-3 applies the text attributes of each sub-element (TITLE, AUTHOR, BINDING, PAGES, and PRICE) to serve as a quick way to obtain the element character data. For example, the following program code is used to retrieve character data in the TITLE element:

Title.innerText=Document.doc umentElement. childNodes (0). text;

The text attribute not only provides the text content of the element represented by the current node, but also the text content of any subsequent element. When the element does not have any child element, this attribute can accurately retrieve the text content (such as the element TITLE) in the element ). However, if the element contains more than one sub-element and character data, the text attribute will return all the text, as shown in the following example (in this example, "Moby-Dick Or, the Whale 」).

Reference content is as follows:
Moby-Dick <br/> <SUBTITLE> Or, the Whale </SUBTITLE> <br/>

To retrieve only the character data of the TITLE element, you must access the subtext node of the TITLE element.

In Table 9-1, the nodeValue attribute of the Element node is null. If the element contains character data, the Text will be stored in the sub-Text node, and you can access the character data from the nodeValue attribute of the Text node. For example, as shown in the previous example, if the Element node of the TITLE Element contains the text "Moby-Dick", the program code below will provide the TITLE's character data "Moby-Dick 」, excluding character data of SUBTITLE:

Element. firstChild. nodeValue

(Because the character data of the TITLE element is prior to its child element, the first child node represents the character data. Therefore, you can use the firstChild attribute to retrieve the character data .) If the character data of an element is scattered with the child element, annotation, or processing command, each individual character data block is represented as the child Text node to which the element belongs. For example, in the following example, the ITEM Element has three subnodes in the order that the Text node represents the first character data block, and the Element node represents the SUB-ITEM, another Text node represents the second character data block:

Reference content is as follows:

Character data block 1
Sub-item text
Character data block 2

Table 9-5 lists the useful attributes and methods provided by Text nodes:

Text node attribute description model

Length the number of characters contained in the Text of this node CharacterCount = Text. length;

Text node method description model

SubstringData (char-offset, num-chars) returns a target string containing a specific number of characters, which comes from the text content of the node.

Char-offset is the starting position of the SubString, and num-chars is the length of the SubString. SubString = Text. substringData (2, 3); (returns the second, third, fourth, and other three characters from the content of the Text element)

The text nodes in Table 9-5 provide a useful attribute and method for distinguishing. the attributes that can be applied also include the common node attributes listed in Table 9-2.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.