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











up vote
0
down vote

favorite
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






















  • Your "new XML" is not well-formed XML. XML requires a root element.
    – Robby Cornelissen
    Nov 22 at 4:20

















up vote
0
down vote

favorite
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






















  • Your "new XML" is not well-formed XML. XML requires a root element.
    – Robby Cornelissen
    Nov 22 at 4:20















up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






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 at 4:18









Nabarun Chakraborti

33




33












  • Your "new XML" is not well-formed XML. XML requires a root element.
    – Robby Cornelissen
    Nov 22 at 4:20




















  • Your "new XML" is not well-formed XML. XML requires a root element.
    – Robby Cornelissen
    Nov 22 at 4:20


















Your "new XML" is not well-formed XML. XML requires a root element.
– Robby Cornelissen
Nov 22 at 4:20






Your "new XML" is not well-formed XML. XML requires a root element.
– Robby Cornelissen
Nov 22 at 4:20














2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










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 at 4:51




















up vote
1
down vote













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





















    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',
    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








    up vote
    0
    down vote



    accepted










    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 at 4:51

















    up vote
    0
    down vote



    accepted










    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 at 4:51















    up vote
    0
    down vote



    accepted







    up vote
    0
    down vote



    accepted






    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 at 4:36









    supl

    895




    895












    • 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 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 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 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 at 4:51














    up vote
    1
    down vote













    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

























      up vote
      1
      down vote













      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























        up vote
        1
        down vote










        up vote
        1
        down vote









        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 at 5:16









        shoonya ek

        214




        214






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks