Parsing XML Using XPATH for Python -
i new python - few days old - , appreciate help.
i want write python code parse below xml below:-
servingcell----------neighbourcell
l41_nbr3347_1----------l41_nbr3347_2
l41_nbr3347_1----------l41_nbr3347_3
l41_nbr3347_1----------l41_nbr3349_1
l41_nbr3347_1----------l41_nbrea2242_1
<ltecell id="l41_nbr3347_1"> <attributes> <abspatterninfotdd><unset/></abspatterninfotdd> <additionalspectrumemission>1</additionalspectrumemission> <additionalspectrumemissionlist><unset/></additionalspectrumemissionlist> <ltespeeddependentconf id="0"> <attributes> <treselectioneutrasfhigh>ldot0</treselectioneut <treselectioneutrasfmedium>ldot0</treselectione </attributes> </ltespeeddependentconf> <lteneighboringcellrelation id="l41_nbr3347_2"> <attributes> <abspatterninfo><unset/></abspatterninfo> </lteneighboringcellrelation> <lteneighboringcellrelation id="l41_nbr3347_3"> <attributes> <abspatterninfo><unset/></abspatterninfo> </lteneighboringcellrelation> <lteneighboringcellrelation id="l41_nbr3349_1"> <attributes> <abspatterninfo><unset/></abspatterninfo> </lteneighboringcellrelation> <lteneighboringcellrelation id="l41_nbrea2242_1"> <attributes> <abspatterninfo><unset/></abspatterninfo> <abspatterninfotdd><unset/></abspatterninfotdd>
first xml path produced not correct: need close tags open. example attributes opened , not closed.
<lteneighboringcellrelation id="l41_nbr3349_1"> <attributes> <abspatterninfo> <unset/> </abspatterninfo> </lteneighboringcellrelation>
i refactored (and simplified little bit) xml correct it.
<ltecell id="l41_nbr3347_1"> <attributes> <lteneighboringcellrelation id="l41_nbr3347_2"> </lteneighboringcellrelation> <lteneighboringcellrelation id="l41_nbr3347_3"> </lteneighboringcellrelation> <lteneighboringcellrelation id="l41_nbr3349_1"> </lteneighboringcellrelation> <lteneighboringcellrelation id="l41_nbrea2242_1"> </lteneighboringcellrelation> </attributes> </ltecell>
here code parse , display it.
import xml.etree.elementtree et tree = et.parse("xmlexample.xml") result = '' root = tree.getroot() e in tree.findall('./attributes/lteneighboringcellrelation'):#attributes/lteneighboringcellrelation print(root.attrib['id']+'----------'+e.attrib.get('id'))
hope helps,
Comments
Post a Comment