XML gets the character data of the element

Source: Internet
Author: User
Tags contains processing instruction access
The script in xml| data listing 9-3 uses the Text property of each child element (TITLE, AUTHOR, BINDING, PAGES, and price) as a quick way to get the element character data. For example, the following program code is used to retrieve character data from the title element:
Title.innertext=document.documentelement.childnodes (0). text;
The Text property provides not only the text content of the element that the current node represents, but also the text content of any subsequent elements. When an element does not have any child elements, the property can correctly retrieve the text content in the element, such as the element title. However, if the element contains more than one child element and character data, the Text property returns all the text, as shown in the following example ("moby-dick or,the whale" in this example).
<title>moby-dick
<subtitle>or,the whale</subtitle>
</TITLE>
In order to obtain only the character data of the title element, you must access the subdocument byte point of the title element.
In table 9-1, the value of the NodeValue property of the Element node is null. If the element contains character data, the text is stored in the Child text node, and you can access the character data from the NodeValue property of the text node. For example, as shown in the previous example, if the element node of the title elements contains literal "moby-dick", the following program code provides the character data "moby-dick" of title without the character data belonging to subtitle:
Element.firstChild.nodeValue
(because the character data position of the title element precedes its child element, the first child node represents the character data, so you can use the FirstChild property to retrieve the character data.) If the character data of an element is interspersed with a child element, annotation, or processing instruction, each individual character data chunk is represented as the child text node to which the element belongs. For example, in the following example, the ITEM element has three child nodes in the order that the Text node represents the first character data block, the element node represents the child element sub-item, and the other Text node represents the second character data block:
<ITEM>
Character Data Block 1
<sub-item>sub-item text</sub-item>
Character Data Block 2
</ITEM>
Table 9-5 lists the useful properties and methods provided by the text node, respectively:
Properties of the Text node Describe Example
Length The number of characters contained in the text of this node Charactercount =text.length;
Method of Text Node Describe Example
Substringdata (Char-offset,num-chars) Returns a string containing a specific number of characters 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); (three characters from the contents of the text element are returned to combined second, etc.)
The table 9-5 text nodes provide a useful property and method, and the properties you can use include the common node properties 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.