Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > XML, XSL > Получение элемента по атрибуту возможно?


Автор: Сириус 24.5.2006, 20:01
Вот имею структуру XML (внизу привел код) и там элементы topic которые отличаются только атрибутом. ВОт как мне выйти на конкретный элемент по его атрибуту? 
еСТЬ КОНЕЧНо способ типа взять и получить массив в стиле
var wins = root.getElementsByTagName("topic");
и далее по циклу пройтись по всем элементам и проверять у кого типа мол данный атрибут равен моему newsName (который хранит атрибут элемента topic который мне нужен)
Но я хочу обойтись без циклов, и почти уверен что это возможно. Короче как сразу выйти на элемент по данному атрибуту?

Код

<?xml version="1.0" encoding="ISO-8859-1"?>

<news>
<topic name="sport" title="Genetal Sport">

    <item topic="Sport1">
Securing shared drives
In Windows 2000, all drives on your computer, such as drive C or D, are automatically shared using the name drive letter$, such as C$ or D$. These drives are not shown with the hand icon that indicates sharing in My Computer or Windows Explorer, and they are also hidden when users connect to your computer remotely.
However, any user can gain access to your computer over a network or the Internet if the user knows your computer name, and the user name and password of a user who is a member of the Administrators, Backup Operators, or Server Operators group. A user who gains access to your drive over the network or Internet can view all folders and files on that drive, even those that are protected using NTFS permissions, provided the NTFS permissions allow access to members of the Administrators, Backup Operators, or Server Operators group.
To keep your drives secure, you should:
    </item>
    <item topic="Sport2">
Libpath
Specifies the directories the OS/2 subsystem is to search for dynamic-link libraries (DLLs). To use this command, use an OS/2 editor to edit the C:\CONFIG.SYS file.
libpath=[drive:]path[;[drive:]path][...] 
Parameters 
drive:
Specifies the drive where DLLs are located. If you do not specify a drive, the OS/2 subsystem searches the disk in the current drive.
path
Specifies the directory to search for DLLs. You can specify more than one directory, separating the names with semicolons (;).
    </item>


    <item topic="Sport3">
Using Paint
Paint is a drawing tool you can use to create black and white or color drawings that you can save as bitmap files. 
Open Paint.
 Notes
To open an accessory, click Start, point to Programs, point to Accessories, and then click the appropriate icon.
For information about using Paint, click the Help menu in Paint. 
    </item>
</topic>


<topic name="computer" title="IT">
    <item topic="Computer life">
Computer news 1;
</item>
<item topic="Computer life">
Computer news 2;
</item>
<item topic="Computer life">
Computer news 3;
</item>
</topic>

<topic name="cinema" title="Movies">
<item topic="News about new movies">
Movie news 1;
</item>
<item topic="News about new movies">
Movie news 2;
</item>
<item topic="News about new movies">
Movie news 3;
</item>
</topic>
</news>
 

Автор: Beard 24.5.2006, 20:13
Через XPath:
Код

//topic[@myAttributeName='newsName']
  

Автор: Сириус 25.5.2006, 08:56
Ну я написал так
var str=newsDocRoot.children.topic[@name='computer'].children.item(1).text;
но вроде ничего не вышло...если не трудно, можешь привести пример? 

Автор: Beard 25.5.2006, 11:19
Сорри - я думал ты работаешь с xml через ActiveXObject и MSXML,
а как "чисто" через JavaScript - не знаю (ну, кроме перебора, конечно smile... 
Жди более продвинутых или спроси в ветке JavaScript 

Автор: Сириус 25.5.2006, 18:03
Хорошо, спасибо   

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)