xml to string and string to xml

蹲街弑〆低调 提交于 2019-12-11 05:49:48

问题


I am trying to convert xml to string and string to xml.

Input xml file:

<?xml version="1.0"?>
<records>
        <record id="1">
                <url>https://www.google.com</url>
                <data>
                        <a xmlns="http://localhost:8080/">
                                <b>1</b>
                                <c>Mayur</c>
                        </a>
                </data>
        </record>
        <record id="2">
                <url>https://www.google.com</url>
                <data>
                        <a xmlns="http://localhost:8080/">
                                <b>2</b>
                                <c>chavda</c>
                        </a>
                </data>
        </record>
</records>

I can't get same xml file when i convert string to xml

tree = ET.parse(xmlfilepath)
root = tree.getroot()
print (tree.tostring(root))

xmlnx moved to records tag.

<records xmlns:ns0="http://localhost:8080/">
            <record id="1">
                    <url>https://www.google.com</url>
                    <data>
                            <ns0:a>
                                    <ns0:b>1</ns0:b>
                                    <ns0:c>Mayur</ns0:c>
                            </ns0:a>
                    </data>
            </record>
            <record id="2">
                    <url>https://www.google.com</url>
                    <data>
                            <ns0:a>
                                    <ns0:b>2</ns0:b>
                                    <ns0:c>chavda</ns0:c>
                            </ns0:a>
                    </data>
            </record>
    </records>

Can i get same xml as output which i have given as a input? What is the reason for changing xml attribute and adding ns0:, ns1: ?

来源:https://stackoverflow.com/questions/41395332/xml-to-string-and-string-to-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!