1

I have these xml code:

<?xml version="1.0" encoding="utf-8"?>
<TAB xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FOLD.xsd">
    <FOLD SERVER="APPLE" VERSION="520" OPERATIVE_SYSTEM="HPUX" FOLD_NAME="CAR" MODIFIED="False" UPL="20211123135822UTC" FOLD_ORDER_METHOD="SYSTEM" REAL_FOLD_ID="154" TYPE="1" USED_BY_CODE="0">
        <JOB ID="443" APPLICATION="CAR" SUB_APPLICATION="SENDGEST" NAMEJO="SESA" CREATED_BY="USERA" USER="DMMM" CRITICAL="0" TASKTYPE="Dummy" CON="0" MXX="0" MRU="0" WD="0,1,2,3,4,5,6" JAN="1" FEB="1" MAR="1" APR="1" MAY="1" JUN="1" JUL="1" AUG="1" SEP="1" OCT="1" NOV="1" DEC="1" DAYS_AND_OR="O" SHIFT="Ignore Job" SHIFTNUM="+00" SYSDB="1" IND_CYCLIC="S" CREATION_USER="USERA" CREATION_DATE="20190829" CREATION_TIME="172439" CHANGE_USERID="USERA" CHANGE_DATE="20200826" CHANGE_TIME="103905" RULE_BASED_CALENDAR_RELATIONSHIP="O" APPL_TYPE="OS" MULTY_AGENT="N" USE_INSTREAM_JCL="N" VERSION_OPCODE="N" CV="Y" VERSION_SERIAL="5">
            <OUT NAME="SESA-TO-SESB" ODATE="ODAT" SIGN="+" />
        </JOB>
        <JOB ID="444" APPLICATION="CAR" SUB_APPLICATION="SENDGEST" NAMEJO="SESB" CREATED_BY="USERA" USER="TO_CAR_P" CRITICAL="0" TASKTYPE="Job" CYCLIC="1" HOST="AFBFTP" INT="00001M" CON="0" RET="0" MW="0" RR="0" AUTOARCH="1" MXX="0" MRU="0" TIMEFROM="0500" TIMETO="0455" WD="0,1,2,3,4,5,6" JAN="1" FEB="1" MAR="1" APR="1" MAY="1" JUN="1" JUL="1" AUG="1" SEP="1" OCT="1" NOV="1" DEC="1" DAYS_AND_OR="O" SHIFT="Ignore Job" SHIFTNUM="+00" SYSDB="1" IND_CYCLIC="S" CREATION_USER="USERA" CREATION_DATE="20190829" CREATION_TIME="172439" CHANGE_USERID="USERA" CHANGE_DATE="20200826" CHANGE_TIME="103905" RULE_BASED_CALENDAR_RELATIONSHIP="O" APPL_TYPE="FILE_TRANS">
            <VAR NAME="PATH" VALUE="NOTAPPLICABLE" />
            <VAR NAME="ACC" VALUE="TO_CAR_P" />
        </JOB>
    </FOLD>
</TAB>

I'm trying to get VAR NAME PATH (only some jobs have these element) with python but I can't extract. I do:

with open(file1, 'rt') as f:

    tree = ElementTree.parse(f)
for movie in root.iter('JOB.PATH'):
    print(movie.attrib)

Any help please? Thanks

3 Answers 3

3

Once you got tree, you can use Xpath notation to search:

for node in tree.iterfind(".//JOB/VAR[@NAME='PATH']"):
    print(node.attrib)

Output:

{'NAME': 'PATH', 'VALUE': 'NOTAPPLICABLE'}

Update:

If you want to filter by both "NAME" and "VALUE" attributes:

for node in tree.iterfind(".//JOB/VAR[@NAME='PATH'][@VALUE='NOTAPPLICABLE']"):
    print(node.attrib)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! A doubt. If I only would like the value. So in that example only wants "NOTAPPLICABLE". How could I do these?
Please see my update.
1

You could search for a VAR nodes and check their PATH

for movie in root.iter("VAR"):
    if movie.attrib["NAME"] == "PATH":
        print("you got me!")

Or using findall

for movie in root.findall(".//VAR/[@NAME='PATH']"):
    print(movie.attrib)

1 Comment

You could also use root.iterfind
1

I think beautifulsoup is much easier.

from bs4 import BeautifulSoup

with open(file1, 'rt') as f:
    soup = BeautifulSoup(f, "xml")
for var in soup.find_all("VAR", NAME="PATH"):
    print(var)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.