⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 e1078. finding elements by attributes in a dom document using xpath.txt

📁 这里面包含了一百多个JAVA源文件
💻 TXT
字号:
XPath is an expression language for selecting nodes in an XML file. See e1074 Finding Elements by Absolute Location in a DOM Document Using XPath for common XPath expression for selecting elements. This example adds to those examples by demonstrating the ability to select elements based on their attributes. 
This example demonstrates some common uses of expressions that use attributes; for more information on XPath, see the specification at http://www.w3c.org/TR/xpath. In the example, the result of an XPath expression is shown next to the expression; the numbers are ids of elements in the sample file shown at the end of the example. 

    // Get all elements where pet equals cat
    String xpath = "//*[@pet='cat']";                // 2
    
    // Get all elements where pet equals dog
    xpath = "//*[@pet='dog']";                       // 7
    // Note that element #3 does not match because the attribute
    // value is " dog " rather than "dog"
    
    // Get all elements where pet contains the string dog
    xpath = "//*[contains(@pet,'dog')]";             // 3 7
    
    // Get all elements that have the age attribute
    xpath = "//*[@age]";                             // 3 6
    
    // Get all elem1 elements that have the age attribute
    xpath = "//elem1[@age]";                         // 3
    
    // Get all elements that have both pet and age attributes
    xpath = "//*[@pet and @age]";                    // 1 3

XPath 1.0 does not support case-insensitive matches. However, a simple case-insensitive match can be done using the translate() function, which converts a string by mapping one character into another: 
    // Get all elements where pet contains the string cat, ignoring case
    xpath = "//*[contains(translate(@pet,'abcdefghijklmnopqrstuvwxyz',"
        + " 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),'CAT')]";  // 2 4

To execute an XPath expression, see e1074 Finding Elements by Absolute Location in a DOM Document Using XPath. Here is the sample XML file used in the example: 
    <?xml version="1.0" encoding="UTF-8"?>
    <root id="1">
        <elem1 id="2" pet="cat"/>
        <elem1 id="3" pet=" dog " age="8"/>
        <elem1 id="4" pet="Cat" >
            <elem2 id="5">
                <elem3 id="6" age="10"/>
            </elem2>
        </elem1>
        <elem1 id="7" pet="dog"/>
    </root>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -