Skip to content

Commit

Permalink
Add Chapter-09
Browse files Browse the repository at this point in the history
  • Loading branch information
SlenderData committed Apr 18, 2024
1 parent cb9023f commit e7db7d2
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Chapter-09/demo-1-origin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<purchaseOrder>
<shipTo country="US">
<name>Alice Smith</name>
<street>123 Maple Street</street>
<city>Mill Valley</city>
<state>CA</state>
<zip>90952</zip>
</shipTo>
<billTo country="US">
<name>Robert Smith</name>
<street>8 Oak Avenue</street>
<city>Old Town</city>
<state>PA</state>
<zip>95819</zip>
</billTo>
<comment>Hurry, my lawn is going wild!</comment>
<items>
<item partNum="872-AA">
<productName>Lawnmower</productName>
<quantity>1</quantity>
<USPrice>148.95</USPrice>
<comment>Confirm this is electric</comment>
</item>
</items>
</purchaseOrder>
57 changes: 57 additions & 0 deletions Chapter-09/demo-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from xml.dom import minidom

# 读取 XML 文件
doc = minidom.parse('demo-1-origin.xml')

# 定义一个函数来移除所有空白文本节点,以便于后续操作
def remove_blank_nodes(node):
# 递归移除所有空白文本节点。
if node.hasChildNodes():
for child in list(node.childNodes):
if child.nodeType == minidom.Node.TEXT_NODE and not child.data.strip():
node.removeChild(child)
else:
remove_blank_nodes(child)

# 移除空白节点
remove_blank_nodes(doc)

# 找到根元素 purchaseOrder
root = doc.documentElement

# 找到 items 元素
items = root.getElementsByTagName('items')[0]

# 创建一个新的 item 元素并设置其属性
new_item = doc.createElement('item')
new_item.setAttribute('partNum', '926-AA')

# 创建并添加 productName 元素
product_name = doc.createElement('productName')
product_name.appendChild(doc.createTextNode('Baby Monitor'))
new_item.appendChild(product_name)

# 创建并添加 quantity 元素
quantity = doc.createElement('quantity')
quantity.appendChild(doc.createTextNode('1'))
new_item.appendChild(quantity)

# 创建并添加 USPrice 元素
us_price = doc.createElement('USPrice')
us_price.appendChild(doc.createTextNode('39.98'))
new_item.appendChild(us_price)

# 创建并添加 shipDate 元素
ship_date = doc.createElement('shipDate')
ship_date.appendChild(doc.createTextNode('1999-05-21'))
new_item.appendChild(ship_date)

# 将新创建的 item 元素添加到 items 元素中
items.appendChild(new_item)

# 输出修改后的 XML 结构
print(doc.toprettyxml())

# 将修改后的 XML 结构写入文件 demo-1-modified.xml
with open('demo-1-modified.xml', 'w') as f:
f.write(doc.toprettyxml())
40 changes: 40 additions & 0 deletions Chapter-09/demo-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import xml.sax

class PurchaseOrderHandler(xml.sax.ContentHandler):
def __init__(self):
self.CurrentData = ""
self.item = None
self.elements = {}

# 元素开始事件处理
def startElement(self, tag, attributes):
self.CurrentData = tag
print(f"元素 {tag} 开始")
if tag == "item":
self.item = {'partNum': attributes['partNum']}
print("item 元素的 partNum 属性值为:", self.item['partNum'])


# 元素结束事件处理
def endElement(self, tag):
print(f"元素 {tag} 结束")

# 内容事件处理
def characters(self, content):
if self.CurrentData and content.strip(): # 避免空白字符触发
print(f"元素 {self.CurrentData} 内的值为: {content.strip()}")
if self.item is not None:
self.item[self.CurrentData] = content.strip()

# 创建一个 XMLReader
parser = xml.sax.make_parser()

# 关闭命名空间
parser.setFeature(xml.sax.handler.feature_namespaces, 0)

# 重写 ContextHandler
Handler = PurchaseOrderHandler()
parser.setContentHandler(Handler)

# 读取并处理 XML 文件
parser.parse('demo-1-origin.xml')
31 changes: 31 additions & 0 deletions Chapter-09/demo-3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 我在第 2 章就完成了符合要求的 python 实现,这里直接复制过来了,做了简单优化
# 这里的代码生成了一个包含 10 个员工信息的 XML 文件

from faker import Faker
import xml.etree.ElementTree as ET
from xml.dom.minidom import parseString

# 创建 Faker 实例,指定中文环境
fake = Faker(locale='zh_CN')

# 创建一个 XML 的根元素
course = ET.Element('course')

# 生成10个学生成绩的信息
for i in range(10):
student = ET.SubElement(course, 'student', number=str(i + 1))
ET.SubElement(student, 'name').text = fake.name()
ET.SubElement(student, 'score').text = str(fake.random_int(min=50, max=100))

# 将生成的XML转换为字节串
xml_bytes = ET.tostring(course, encoding='utf-8', method='xml')

# 使用minidom解析字符串
dom = parseString(xml_bytes)

# 打印格式化后的字符串
print(dom.toprettyxml())

# 将格式化后的XML字符串写入文件
with open('demo-3.xml', 'wb') as f:
f.write(dom.toprettyxml().encode('utf-8'))

0 comments on commit e7db7d2

Please sign in to comment.