How to remove Root tag and keep rest all row tags in an xml using python












0















I've the below XML file.



<root>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
<catalog>
<book id="bk102">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>45.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
<catalog>
<book id="bk103">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>46.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
</root>


I want to create another XML by eliminating the tag. So, my new XML will look like -



<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
<catalog>
<book id="bk102">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>45.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
<catalog>
<book id="bk103">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>46.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>


Below is my code and I'm able to generate byte class by eliminating the and keeping all the necessary row tags. but finally not able to convert my byte class to an xml format and getting the below error :



xml.etree.ElementTree.ParseError: junk after document element: line 11, column 0



Can you please assist?



import xml.etree.ElementTree as ET

base_tree = ET.parse('input.xml')
catalog = list(base_tree.getroot())
elemList =
for elem in catalog:
getele = ET.tostring(elem, 'utf-8')
elemList.append(getele)

byt = b''.join(elemList)
print(byt)

mytree = ET.ElementTree(ET.fromstring(byt))
dis = str(ET.tostring(mytree.getroot()), 'utf-8')









share|improve this question























  • Your "new XML" is not well-formed XML. XML requires a root element.

    – Robby Cornelissen
    Nov 22 '18 at 4:20


















0















I've the below XML file.



<root>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
<catalog>
<book id="bk102">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>45.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
<catalog>
<book id="bk103">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>46.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
</root>


I want to create another XML by eliminating the tag. So, my new XML will look like -



<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
<catalog>
<book id="bk102">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>45.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
<catalog>
<book id="bk103">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>46.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>


Below is my code and I'm able to generate byte class by eliminating the and keeping all the necessary row tags. but finally not able to convert my byte class to an xml format and getting the below error :



xml.etree.ElementTree.ParseError: junk after document element: line 11, column 0



Can you please assist?



import xml.etree.ElementTree as ET

base_tree = ET.parse('input.xml')
catalog = list(base_tree.getroot())
elemList =
for elem in catalog:
getele = ET.tostring(elem, 'utf-8')
elemList.append(getele)

byt = b''.join(elemList)
print(byt)

mytree = ET.ElementTree(ET.fromstring(byt))
dis = str(ET.tostring(mytree.getroot()), 'utf-8')









share|improve this question























  • Your "new XML" is not well-formed XML. XML requires a root element.

    – Robby Cornelissen
    Nov 22 '18 at 4:20
















0












0








0


1






I've the below XML file.



<root>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
<catalog>
<book id="bk102">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>45.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
<catalog>
<book id="bk103">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>46.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
</root>


I want to create another XML by eliminating the tag. So, my new XML will look like -



<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
<catalog>
<book id="bk102">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>45.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
<catalog>
<book id="bk103">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>46.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>


Below is my code and I'm able to generate byte class by eliminating the and keeping all the necessary row tags. but finally not able to convert my byte class to an xml format and getting the below error :



xml.etree.ElementTree.ParseError: junk after document element: line 11, column 0



Can you please assist?



import xml.etree.ElementTree as ET

base_tree = ET.parse('input.xml')
catalog = list(base_tree.getroot())
elemList =
for elem in catalog:
getele = ET.tostring(elem, 'utf-8')
elemList.append(getele)

byt = b''.join(elemList)
print(byt)

mytree = ET.ElementTree(ET.fromstring(byt))
dis = str(ET.tostring(mytree.getroot()), 'utf-8')









share|improve this question














I've the below XML file.



<root>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
<catalog>
<book id="bk102">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>45.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
<catalog>
<book id="bk103">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>46.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
</root>


I want to create another XML by eliminating the tag. So, my new XML will look like -



<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
<catalog>
<book id="bk102">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>45.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
<catalog>
<book id="bk103">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>46.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>


Below is my code and I'm able to generate byte class by eliminating the and keeping all the necessary row tags. but finally not able to convert my byte class to an xml format and getting the below error :



xml.etree.ElementTree.ParseError: junk after document element: line 11, column 0



Can you please assist?



import xml.etree.ElementTree as ET

base_tree = ET.parse('input.xml')
catalog = list(base_tree.getroot())
elemList =
for elem in catalog:
getele = ET.tostring(elem, 'utf-8')
elemList.append(getele)

byt = b''.join(elemList)
print(byt)

mytree = ET.ElementTree(ET.fromstring(byt))
dis = str(ET.tostring(mytree.getroot()), 'utf-8')






python python-3.x python-2.7






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 4:18









Nabarun ChakrabortiNabarun Chakraborti

63




63













  • Your "new XML" is not well-formed XML. XML requires a root element.

    – Robby Cornelissen
    Nov 22 '18 at 4:20





















  • Your "new XML" is not well-formed XML. XML requires a root element.

    – Robby Cornelissen
    Nov 22 '18 at 4:20



















Your "new XML" is not well-formed XML. XML requires a root element.

– Robby Cornelissen
Nov 22 '18 at 4:20







Your "new XML" is not well-formed XML. XML requires a root element.

– Robby Cornelissen
Nov 22 '18 at 4:20














2 Answers
2






active

oldest

votes


















1














You can use list for this.



with open('input.xml') as input_file:
text = input_file.read()
catalog = list(ET.fromstring(text))[0]
ET.tostring(catalog, encoding='utf8', method='xml')


Though resulting string will not be a valid XML.






share|improve this answer































    0














    root element is mandatory for being XML.



    For just text processing maybe we could just do



    import re
    pattern = re.compile("<[/]{0,1}root>")
    removed = re.sub(pattern, '', "<root>something</root>");

    print(removed)


    ?






    share|improve this answer
























    • But then how will you solve the second problem ? regex.info/blog/2006-09-15/247 :). I would avoid regex when I have some structure like XML.

      – 0xc0de
      Nov 22 '18 at 4:51













    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423840%2fhow-to-remove-root-tag-and-keep-rest-all-row-tags-in-an-xml-using-python%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can use list for this.



    with open('input.xml') as input_file:
    text = input_file.read()
    catalog = list(ET.fromstring(text))[0]
    ET.tostring(catalog, encoding='utf8', method='xml')


    Though resulting string will not be a valid XML.






    share|improve this answer




























      1














      You can use list for this.



      with open('input.xml') as input_file:
      text = input_file.read()
      catalog = list(ET.fromstring(text))[0]
      ET.tostring(catalog, encoding='utf8', method='xml')


      Though resulting string will not be a valid XML.






      share|improve this answer


























        1












        1








        1







        You can use list for this.



        with open('input.xml') as input_file:
        text = input_file.read()
        catalog = list(ET.fromstring(text))[0]
        ET.tostring(catalog, encoding='utf8', method='xml')


        Though resulting string will not be a valid XML.






        share|improve this answer













        You can use list for this.



        with open('input.xml') as input_file:
        text = input_file.read()
        catalog = list(ET.fromstring(text))[0]
        ET.tostring(catalog, encoding='utf8', method='xml')


        Though resulting string will not be a valid XML.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 5:16









        shoonya ekshoonya ek

        365




        365

























            0














            root element is mandatory for being XML.



            For just text processing maybe we could just do



            import re
            pattern = re.compile("<[/]{0,1}root>")
            removed = re.sub(pattern, '', "<root>something</root>");

            print(removed)


            ?






            share|improve this answer
























            • But then how will you solve the second problem ? regex.info/blog/2006-09-15/247 :). I would avoid regex when I have some structure like XML.

              – 0xc0de
              Nov 22 '18 at 4:51


















            0














            root element is mandatory for being XML.



            For just text processing maybe we could just do



            import re
            pattern = re.compile("<[/]{0,1}root>")
            removed = re.sub(pattern, '', "<root>something</root>");

            print(removed)


            ?






            share|improve this answer
























            • But then how will you solve the second problem ? regex.info/blog/2006-09-15/247 :). I would avoid regex when I have some structure like XML.

              – 0xc0de
              Nov 22 '18 at 4:51
















            0












            0








            0







            root element is mandatory for being XML.



            For just text processing maybe we could just do



            import re
            pattern = re.compile("<[/]{0,1}root>")
            removed = re.sub(pattern, '', "<root>something</root>");

            print(removed)


            ?






            share|improve this answer













            root element is mandatory for being XML.



            For just text processing maybe we could just do



            import re
            pattern = re.compile("<[/]{0,1}root>")
            removed = re.sub(pattern, '', "<root>something</root>");

            print(removed)


            ?







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 '18 at 4:36









            suplsupl

            745




            745













            • But then how will you solve the second problem ? regex.info/blog/2006-09-15/247 :). I would avoid regex when I have some structure like XML.

              – 0xc0de
              Nov 22 '18 at 4:51





















            • But then how will you solve the second problem ? regex.info/blog/2006-09-15/247 :). I would avoid regex when I have some structure like XML.

              – 0xc0de
              Nov 22 '18 at 4:51



















            But then how will you solve the second problem ? regex.info/blog/2006-09-15/247 :). I would avoid regex when I have some structure like XML.

            – 0xc0de
            Nov 22 '18 at 4:51







            But then how will you solve the second problem ? regex.info/blog/2006-09-15/247 :). I would avoid regex when I have some structure like XML.

            – 0xc0de
            Nov 22 '18 at 4:51




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423840%2fhow-to-remove-root-tag-and-keep-rest-all-row-tags-in-an-xml-using-python%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith