×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • 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!
  • Students Click Here

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

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

extract Data from XML file

extract Data from XML file

extract Data from XML file

(OP)
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>

RE: extract Data from XML file

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 

RE: extract Data from XML file

(OP)
Mikrom,

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

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!

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