How to remove Root tag and keep rest all row tags in an xml using python
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
add a comment |
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
Your "new XML" is not well-formed XML. XML requires a root element.
– Robby Cornelissen
Nov 22 '18 at 4:20
add a comment |
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
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
python python-3.x python-2.7
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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)
?
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 22 '18 at 5:16


shoonya ekshoonya ek
365
365
add a comment |
add a comment |
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)
?
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
add a comment |
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)
?
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
add a comment |
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)
?
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)
?
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Your "new XML" is not well-formed XML. XML requires a root element.
– Robby Cornelissen
Nov 22 '18 at 4:20