How to remove Root tag and keep rest all row tags in an xml using python
up vote
0
down vote
favorite
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 |
up vote
0
down vote
favorite
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 at 4:20
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 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
add a comment |
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
add a comment |
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)
?
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
add a comment |
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.
add a comment |
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)
?
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
add a comment |
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)
?
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
add a comment |
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)
?
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 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
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 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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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 at 5:16
shoonya ek
214
214
add a comment |
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.
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.
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 at 4:20