Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"Because of this forum, I continue to WOW! my clients!"

Geography

Where in the world do Tek-Tips members come from?

extract Data from XML fileHelpful Member! 

hogie503 (Programmer)
20 Jul 12 15:14
I would like help on the best way to extract the highlighted data from the XML file below this data will then be loaded in a Database any language is fine I am tryihg in C# or PHP, The info is in RED
Any help will be greatly appreciated.... Thanks


<?xml version="1.0" encoding="ISO-8859-1"?>
<Workstation Number="80033" CompType="0" Type="baseline" UserDef1="Skq34" UserDef2="89098" Userdef3="76" Comment="This is a test" >
<Hardware>
<Computer>
<Name>SPKCDC1A84</Name>
<Manufacturer>INTELR</Manufacturer>
<Model>ADRADCPI</Model>
<ServiceTag />
<BiosName>AT/AT Comp</BiosName>
</Computer>

<CPU>
<Installed>2</Installed>
<Vendor>Intel</Vendor>
<Speed>2400</Speed>
<Model>Awardacpi</Model>
<SerialNumber>000-949-0000</SerialNumber>
<CPUFamily>15</CPUFamily>
</CPU>

<Drive>
<Name>A:</Name>
<Description/>
<Capacity>0</Capacity>
<Free>0</Free>
<Type>Removable</Type>
</Drive>

<Drive>
<Name>C:</Name>
<Description/>
<Capacity>99999</Capacity>
<Free>8888888</Free>
<Type>Fixed</Type>
</Drive>

<Drive>
<Name>D:</Name>
<Description/>
<Capacity>55555555</Capacity>
<Free>4444444</Free>
<Type>Fixed</Type>
</Drive>

<Drive>
<Name>F:</Name>
<Description> DVD_2345</Description>
<Capacity>0</Capacity>
<Type>Removable</Type>
</Drive>

</Hardware>
</Workstation>
Helpful Member!  mikrom (Programmer)
23 Jul 12 5:01
Here is an simple example how to do it in VBscript
workstation_parse.vbs

CODE

set xml_doc = CreateObject("Microsoft.XMLDOM")

xml_doc.load("workstation.xml")

'parse Workstation attributes
set node = xml_doc.selectSingleNode("/Workstation")

attr_id = "Number"
attr_val = node.getAttribute(attr_id)
wscript.echo("Workstation " & attr_id & "= " & attr_val) 

attr_id = "UserDef1"
attr_val = node.getAttribute(attr_id)
wscript.echo("Workstation " & attr_id & "= " & attr_val) 

attr_id = "UserDef2"
attr_val = node.getAttribute(attr_id)
wscript.echo("Workstation " & attr_id & "= " & attr_val) 

'parse Computer Name and Model
set node = xml_doc.selectSingleNode("/Workstation/Hardware/Computer/Name")
wscript.echo("Computer Name" & "= " & node.Text)

set node = xml_doc.selectSingleNode("/Workstation/Hardware/Computer/Model")
wscript.echo("Computer Model" & "= " & node.Text)

'parse CPU Speed and Model
set node = xml_doc.selectSingleNode("/Workstation/Hardware/CPU/Speed")
wscript.echo("CPU Speed" & "= " & node.Text)

set node = xml_doc.selectSingleNode("/Workstation/Hardware/CPU/Model")
wscript.echo("CPU Model" & "= " & node.Text)

'parse all drives
wscript.echo("Drives:")

set drives = xml_doc.getElementsByTagName("Drive")
for each drive in drives
  for each drive_data in drive.ChildNodes
    select case drive_data.NodeName
      case "Name"
        drive_name = drive_data.Text
      case "Description"
        drive_description = drive_data.Text
      case "Capacity"
        drive_capacity = drive_data.Text
      case "Free"
        drive_free = drive_data.Text
    end select
  next
  'print drive data
  if drive_name <> "A:" then
    if drive_Description  = "" then
      drive_info = "  Name " & drive_name & _
                   ", Capacity: " & drive_capacity & _
                   ", Free: " & drive_free
    else
      drive_info = "  Name " & drive_name & _
                   ", Description: " & drive_description
    end if
    wscript.echo(drive_info)
  end if  
next

'at end release XMLDOM object from memory
set xml_doc = nothing 
Output:

CODE

C:\_mikrom\Work>cscript /NoLogo workstation_parse.vbs
Workstation Number= 80033
Workstation UserDef1= Skq34
Workstation UserDef2= 89098
Computer Name= SPKCDC1A84
Computer Model= ADRADCPI
CPU Speed= 2400
CPU Model= Awardacpi
Drives:
  Name C:, Capacity: 99999, Free: 8888888
  Name D:, Capacity: 55555555, Free: 4444444
  Name F:, Description: DVD_2345 
hogie503 (Programmer)
24 Jul 12 23:16
Mikrom,

Thanks you so much for the example thats a GREAT example, just what I needed!!!!

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close