1

I am using lxml in an attempt to to output the following xml code:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <students>
        <!-- 
            学生信息表
            "id" : [名字, 数学, 语文, 英文]
        -->
        {
        "1" : ["张三", 150, 120, 100],
        "2" : ["李四", 90, 99, 95],
        "3" : ["王五", 60, 66, 68]
        }
    </students>
</root> 

This is my code, but the output is not correct:

from lxml import etree
with open('student.txt', 'r') as f:
    data = f.read()
root = etree.Element("root")
child1 = etree.SubElement(root, "students" )
child1.addprevious(etree.Comment('学生信息表 \n "id" : [名字, 数学, 语文, 英文]'))
child1.text = str(data)

# write to file:
tree = etree.ElementTree(root)
tree.write('student.xml', pretty_print=True, xml_declaration=True, encoding='utf-8')

The output xml is like this; comment is not in element <students>:

<?xml version='1.0' encoding='UTF-8'?>
<root>
    <!--学生信息表 
   "id" : [名字, 数学, 语文, 英文]-->
    <students>{
        "1":["张三",150,120,100],
        "2":["李四",90,99,95],
        "3":["王五",60,66,68]
        }
    </students>
</root>

Please help me out on what I am doing wrong and how to proceed.

3
  • Maybe you would like to share with us what should be the correct output? If the correct output is trivial then would you like to share with us debugging details? Commented Nov 20, 2020 at 7:31
  • 1
    To make the code a proper minimal reproducible example, you need to show us the content of student.txt. Commented Nov 20, 2020 at 9:05
  • the content of student.txt : { "1" : ["张三", 150, 120, 100], "2" : ["李四", 90, 99, 95], "3" : ["王五", 60, 66, 68] } Commented Nov 21, 2020 at 12:52

2 Answers 2

0

Try it this way:

students = """<?xml version="1.0" encoding="UTF-8"?>
<root>
    <students>        
        </students>
    </root> """

student_txt = """{ "1" : ["张三", 150, 120, 100], "2" : ["李四", 90, 99, 95], "3" : ["王五", 60, 66, 68]} """
    
from lxml import etree
doc = etree.XML(students.encode())

destination = doc.xpath('//students')[0]
destination.text=''    
new_comment = etree.Comment(student_txt)    
new_comment.tail = "\n"+student_txt

destination.insert(0, new_comment)
print(etree.tostring(doc).decode())

Output:

<root>
    <students><!--{ "1" : ["张三", 150, 120, 100], "2" : ["李四", 90, 99, 95], "3" : ["王五", 60, 66, 68]} -->
{ "1" : ["张三", 150, 120, 100], "2" : ["李四", 90, 99, 95], "3" : ["王五", 60, 66, 68]} </students>
</root>
Sign up to request clarification or add additional context in comments.

1 Comment

new_comment = etree.Comment('学生信息表 \n "id" : [名字, 数学, 语文, 英文]')
0

Instead of using addprevious(), you should use insert as follows:

child1.insert(0, etree.Comment('学生信息表 \n "id" : [名字, 数学, 语文, 英文]'))

1 Comment

thks,I use this way,but the comment at the end of the element.I want before the element.How to?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.