Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple - getting attibutes?

Status
Not open for further replies.

fixthebug2003

Programmer
Joined
Oct 20, 2003
Messages
294
Location
US
Hi,
here is a simple XML structure

<bus busid="7821" protocol="NPS">
<circuit>ESDMP</circuit>
<voltage>12</voltage>
<ampere>7</ampere>
</bus>

how can I get the "busid" from the first element?

fixthebug2003
 
Which XML DOM parser? In which language or IDE or environment? In generic XML "theory", an attribute is a node. It's an attribute node instead of an element node.

My point is, "getting" anything out of XML depends on what/who is doing the getting.



Thomas D. Greer
Providing PostScript & PDF
Training, Development & Consulting
 
Ok, we're getting closer. You're using MSXML Parser in what language? Visual Basic? MSXML supports Xpath, and in Xpath attributes are preceded by the "@" symbol.

So if you wanted to select all attributes named "protocol", you would use "//@protocol".

If you want the VALUE of an attribute, you can use the "=" expression: //bus[@procotol='NPS'

But again, you have several levels to go through. You have to learn how the language you're using (VB?) passes Xpath instructions to the parser and what it returns as results.

Thomas D. Greer
Providing PostScript & PDF
Training, Development & Consulting
 
Hi tgreer,
Sounds like parsing an XML document using, say MS XML Parser, is different in VB and JavaScript, C++ etc..
Is that what you're saying?
Fixthebug2003
 
I'm saying that you have to use SOMETHING to "process" the XML file. MSXML exposes XML documents,schemas, XPath, etc through a COM-based interface to your language.

So you have several layers: how does the language you're using support COM? In other words, you have to use the MSXML Parser, and there are steps involved to do that.

Then you have to decide how you're going to use the parser to interact with your document. You can use SAX or XMLDOM. You can use XPATH, but you have to know how to construct Xpath statements in your language, and how to pass them to the methods exposed by XML Parser, and how to get results back.

But I think the generic answer to your question, is that in Xpath statements, which is the "standard" way of interacting with XML documents, attributes are referenced using the "@" character.

But you certainly don't have to use xpath. You could just as easily do standard file io, looking for the "=" sign. In other words, access attributes however you like!

Or, in .NET, you could use the XMLReader object with its properties such as .HasAttributes and its methods such as .MoveToNextAttribute, reading with .Name and/or .Value. Don't even use a parser, use the built-in reader object.

Your question, "how do I get an attribute" is answered by "it depends... what is doing the getting?"




Thomas D. Greer
Providing PostScript & PDF
Training, Development & Consulting
 
tgreer is correct - there's (at least) two main methods of processing XML in the MSXML 3 parser (SAX, and loading the XML into a DOM document). The differences are significant.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top