Solved

parsing an xml file

Posted on 2013-06-27
3
297 Views
Last Modified: 2013-07-08
Hi all, I need a quick process to parse an XML file that has the below and I need to grab identify what the DEVICE number is attached to the SN. so per below I will need to look for the sn (405840461011130000) the value is below on line 6 and find its device ID on line 2, the value I'm looking for is the device the SN will be provided but the XML will have hundreds of them and I will need to find a specific SN and then get its device

<xmlroot>
<type value="DEVICE001">
<property id="PortType" valuetype="number" value="3"/>
<property id="DeviceID" valuetype="number" value="32"/>
<property id="ModelName" valuetype="string" value="TM-T88V"/>
<property id="PortName" valuetype="string" value="\\?\USB#VID_04b8&amp;PID_0202#405840461011130000"/>
<property id="DeviceType" valuetype="number" value="0"/>
<custom id="TM.PrinterID.2" valuetype="number" value="2"/>
0
Comment
Question by:atorex
3 Comments
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 39282925
You could try something like this:
sn='405840461011130000'
awk  -F'[=|>]' -v sn="$sn" '
$0 ~ "<type value=" {d=$2}
$0 ~ sn {print sn" = "d;}
' test.xml

Open in new window

0
 

Author Comment

by:atorex
ID: 39284068
Thanks,
I will test this out ASAP.

regards,
0
 
LVL 26

Accepted Solution

by:
skullnobrains earned 500 total points
ID: 39289575
this is a bit ugly, but should be rather fast

cat xml | sed -n '1,/405840461011130000/ s/.*DEVICE\([0-9]*\).*/\1/p' | tail -n 1

the sed extracts and prints all the ids until it sees the required SN, the tail makes sure only the last one gets printed on screen
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now