0

So, I'm trying to parse the xml and taking a value on the commandline by python. Here's the code that pretty much explains itself.

APPLY_DIR=$(python -c 'from xml.dom import minidom;xmldoc=minidom.parse("args.xml");itemlist=xmldoc.getElementsByTagName("item");print str(itemlist[6].attributes["patchApplyDir"])')
REVERT_DIR=$(python -c 'from xml.dom import minidom;xmldoc=minidom.parse("args.xml");itemlist=xmldoc.getElementsByTagName("item");print str(itemlist[7].attributes["patchRevertDir"])')

echo $APPLY_DIR
echo $REVERT_DIR

The problem is that it returns the following output:

<xml.dom.minidom.Attr instance at 0x7fba97db07e8>
<xml.dom.minidom.Attr instance at 0x7fb820e80998>

Here's my xml file args.xml

<data>
        <args>
                <item   svn_repo = "https://ntil-svn-u1:18080/svn/HSAN_SW_DEVELOPMENT/HSAN-SW-DEV/Project/Engineering/Code/HSAN_INTEGRATION/HSAN_MERGE/Tags/HSAN_ST_RELEASE_16_12_2016_"></item>
                <item   svn_revision = "991/"></item>
                <item   checkout_path = "/root/Neeraj/GGG"></item>
                <item   pre_build = "Default1"></item>
                <item   build_script = "Default1"></item>
                <item   svnCheckoutPath = "/root/Neeraj/GGG"></item>
                <item   patchApplyDir = "./GGG/Apply"></item>
                <item   patchRevertDir = "./GGG/Revert"></item>
                <item   APPLY_DIR="./Apply"></item>
                <item   REVERT_DIR="./Revert"></item>
                <item   VERSION_MINOR="100"></item>
                <item   VERSION_INTERNAL="200"></item>
                <item INPUT="300"></item>
        </args>
</data>

Please let me know what am I doing wrong here? Also, I don't want to make a separate file to write python code, I have to do it in the command only. Thanks.

आनंद
  • 2,472
  • 4
  • 21
  • 25

2 Answers2

2

If you want the value of the selected item, use .value:

APPLY_DIR=$(python -c 'from xml.dom import minidom;xmldoc=minidom.parse("args.xml");itemlist=xmldoc.getElementsByTagName("item");print itemlist[6].attributes["patchApplyDir"].value')
REVERT_DIR=$(python -c 'from xml.dom import minidom;xmldoc=minidom.parse("args.xml");itemlist=xmldoc.getElementsByTagName("item");print itemlist[7].attributes["patchRevertDir"].value')

echo $APPLY_DIR
# ./GGG/Apply

echo $REVERT_DIR
# ./GGG/Revert
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
1

If you need the value then, try doing attributes['attribute'].value

Like:

APPLY_DIR=$(python -c 'from xml.dom import minidom;xmldoc=minidom.parse("args.xml");itemlist=xmldoc.getElementsByTagName("item");print str(itemlist[6].attributes["patchApplyDir"].value)')

On phone, could not test it. Try and reply back.

Vikash Kumar
  • 145
  • 1
  • 10