xml file handling

Asked 2 years ago, Updated 2 years ago, 79 views

<?xml version="1.0" encoding="UTF-8"?><response>
<header>
<resultCode>INFO-00</resultCode>
<resultMsg>NORMAL SERVICE</resultMsg>
</header>
<body>
<items>
<item>
<sgId>20180613</sgId>
<sgTypecode>4</sgTypecode>
<huboid>100000000</huboid>
<sggName>Jongno-gu</sggName>
<sdName>Seoul Metropolitan Government</sdName>
<wiwName>Jongno-gu</wiwName>
<jdName>Unaffiliated</jdName>
<name>Hong Gil-dong</name>
<hanjaName>洪吉童</hanjaName>
</item>
</items>
</body>

If the xml file looks like this,

from bs4 import BeautifulSoup as bs

f = open ('file above', 'r', encoding = 'UTF-8')

l = f.read()

soup = bs(l, 'lxml')


     if x in soup.findAll('item')

         print(x.find('sgld'))

Why does the result value of this function show none? How do I get back?

python xml

2022-09-22 08:56

1 Answers

Look at the code carefully before asking questions. If you post it here without thinking it's impossible, the questioner's skills will not improve.

The code written by the questioner has two grammatical problems. The grammatical problem means that I haven't checked the error message properly.

First of all

The created xml is not validated.

xml must have only one root. However, <response> is missing even though <response> is root.

Secondly, we're confusing lower case letters with upper case letters I. Also, it is changed to lowercase by lxml, so you need to find sgid.

print(x.find('sgld'))    # sgid

Make sure to make indentation a habit when writing code in the form of markup (html, xml)

xml = '''<?xml version="1.0" encoding="UTF-8"?>
<response>
    <header>
        <resultCode>INFO-00</resultCode>
        <resultMsg>NORMAL SERVICE</resultMsg>
    </header>
    <body>
        <items>
            <item>
                <sgId>20180613</sgId>
                <sgTypecode>4</sgTypecode>
                <huboid>100000000</huboid>
                <sggName>Jongno-gu</sggName>
                <sdName>Seoul Metropolitan Government</sdName>
                <wiwName>Jongno-gu</wiwName>
                <jdName>Unaffiliated</jdName>
                <name>Hong Gil-dong</name>
                <hanjaName>洪吉童</hanjaName>
            </item>
        </items>
    </body>
</response>'''

from bs4 import BeautifulSoup as bs
soup = bs(xml, 'lxml')
for x in soup.findAll('item'):
    print(x.find('sgid'))


2022-09-22 08:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.