Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

parsing an xml file

Posted on 2013-06-27
3
Medium Priority
?
305 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 27

Accepted Solution

by:
skullnobrains earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Fine Tune your automatic Updates for Ubuntu / Debian
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

916 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