nodeName,nodeValue未知 xml 入库方案 The ElementTree iterparse Function
import xml.etree.ElementTree as ET
from lxml.html import *
from xmljson import badgerfish as bf
from pymongo import * xmlDict = {}
def RecursionDict(dict_a):
if isinstance(dict_a,dict):
for x in range(0,len(dict_a)):
temp_key = dict_a.keys()[x]
temp_value = dict_a[temp_key]
if isinstance(temp_value,dict) and len(temp_value)== 1 :
w = temp_value[temp_value.keys()[0]]
if not isinstance(w,dict):
xmlDict[temp_key] = w
RecursionDict(temp_value) tree = ET.parse('listorderitems1493779131.xml')
root = tree.getroot()
xmlstr = ET.tostring(root, "us-ascii", "xml")
print xmlstr res = bf.data(fromstring(xmlstr))
print res
RecursionDict(res)
print xmlDict
client = MongoClient()
db = client.apixmldict
collection = db.col
collection.save(xmlDict)
wuser@ubuntu:~/apiamzpy$ cat wxmljsondict.py
import xml.etree.ElementTree as ET
from lxml.html import *
from xmljson import badgerfish as bf
from pymongo import * xmlDict = {}
def RecursionDict(dict_a):
if isinstance(dict_a,dict):
for x in range(,len(dict_a)):
temp_key = dict_a.keys()[x]
temp_value = dict_a[temp_key]
if isinstance(temp_value,dict) and len(temp_value)== :
w = temp_value[temp_value.keys()[]]
if not isinstance(w,dict):
xmlDict[temp_key] = w
RecursionDict(temp_value) tree = ET.parse('listorderitems1493779131.xml')
root = tree.getroot()
xmlstr = ET.tostring(root, "us-ascii", "xml")
print xmlstr res = bf.data(fromstring(xmlstr))
print res
RecursionDict(res)
print xmlDict
client = MongoClient()
db = client.apixmldict
collection = db.col
collection.save(xmlDict)
wuser@ubuntu:~/apiamzpy$ cat listorderitems1493779131.xml
<?xml version="1.0"?>
<ListOrderItemsResponse xmlns="https://mws.amazonservices.com/Orders/2013-09-01">
<ListOrderItemsResult>
<AmazonOrderId>--</AmazonOrderId>
<OrderItems>
<OrderItem>
<ASIN>B01M123ABC</ASIN>
<SellerSKU>ABCEHM054AWUS-USAS2</SellerSKU>
<OrderItemId></OrderItemId>
<Title>wwwTEST_DATA_Holife Vacuum Sealer, Silver Compact Food Saver Wet/Dry Vacuum Sealing System with Food Grade Starter Bags</Title>
<QuantityOrdered></QuantityOrdered>
<QuantityShipped></QuantityShipped>
<PromotionIds/>
</OrderItem>
</OrderItems>
</ListOrderItemsResult>
<ResponseMetadata>
<RequestId>8cc6b5dc-f79e-4da4-b914-9f14388c0bbf</RequestId>
</ResponseMetadata>
</ListOrderItemsResponse>
wuser@ubuntu:~/apiamzpy$
> db.col.save({'w':})
WriteResult({ "nInserted" : })
> db.col.find().pretty()
{ "_id" : ObjectId("590b35877511f2683d345653"), "w" : }
> db.col.find().pretty()
{ "_id" : ObjectId("590b35877511f2683d345653"), "w" : }
{
"_id" : ObjectId("590b35ab1d41c832e2b6048b"),
"exception" : "<ns0:ListOrderItemsResponse xmlns:ns0=\"https://mws.amazonservices.com/Orders/2013-09-01\">\n <ns0:ListOrderItemsResult>\n <ns0:AmazonOrderId>123-1239963-8862642</ns0:AmazonOrderId>\n <ns0:OrderItems>\n <ns0:OrderItem>\n <ns0:ASIN>B01M123ABC</ns0:ASIN>\n <ns0:SellerSKU>ABCEHM054AWUS-USAS2</ns0:SellerSKU>\n <ns0:OrderItemId>12325810562154</ns0:OrderItemId>\n <ns0:Title>wwwTEST_DATA_Holife Vacuum Sealer, Silver Compact Food Saver Wet/Dry Vacuum Sealing System with Food Grade Starter Bags</ns0:Title>\n <ns0:QuantityOrdered>1</ns0:QuantityOrdered>\n <ns0:QuantityShipped>0</ns0:QuantityShipped>\n <ns0:PromotionIds />\n </ns0:OrderItem>\n </ns0:OrderItems>\n </ns0:ListOrderItemsResult>\n <ns0:ResponseMetadata>\n <ns0:RequestId>8cc6b5dc-f79e-4da4-b914-9f14388c0bbf</ns0:RequestId>\n </ns0:ResponseMetadata>\n</ns0:ListOrderItemsResponse>"
}
{
"_id" : ObjectId("590b35ab1d41c832e2b6048c"),
"asin" : "B01M123ABC",
"sellersku" : "ABCEHM054AWUS-USAS2",
"title" : "wwwTEST_DATA_Holife Vacuum Sealer, Silver Compact Food Saver Wet/Dry Vacuum Sealing System with Food Grade Starter Bags",
"amazonorderid" : "123-1239963-8862642",
"quantityshipped" : ,
"requestid" : "8cc6b5dc-f79e-4da4-b914-9f14388c0bbf",
"quantityordered" : ,
"orderitemid" : NumberLong("")
}
> db.col.find().pretty()
{ "_id" : ObjectId("590b35877511f2683d345653"), "w" : }
{
"_id" : ObjectId("590b35ab1d41c832e2b6048b"),
"exception" : "<ns0:ListOrderItemsResponse xmlns:ns0=\"https://mws.amazonservices.com/Orders/2013-09-01\">\n <ns0:ListOrderItemsResult>\n <ns0:AmazonOrderId>123-1239963-8862642</ns0:AmazonOrderId>\n <ns0:OrderItems>\n <ns0:OrderItem>\n <ns0:ASIN>B01M123ABC</ns0:ASIN>\n <ns0:SellerSKU>ABCEHM054AWUS-USAS2</ns0:SellerSKU>\n <ns0:OrderItemId>12325810562154</ns0:OrderItemId>\n <ns0:Title>wwwTEST_DATA_Holife Vacuum Sealer, Silver Compact Food Saver Wet/Dry Vacuum Sealing System with Food Grade Starter Bags</ns0:Title>\n <ns0:QuantityOrdered>1</ns0:QuantityOrdered>\n <ns0:QuantityShipped>0</ns0:QuantityShipped>\n <ns0:PromotionIds />\n </ns0:OrderItem>\n </ns0:OrderItems>\n </ns0:ListOrderItemsResult>\n <ns0:ResponseMetadata>\n <ns0:RequestId>8cc6b5dc-f79e-4da4-b914-9f14388c0bbf</ns0:RequestId>\n </ns0:ResponseMetadata>\n</ns0:ListOrderItemsResponse>"
}
{
"_id" : ObjectId("590b35ab1d41c832e2b6048c"),
"asin" : "B01M123ABC",
"sellersku" : "ABCEHM054AWUS-USAS2",
"title" : "wwwTEST_DATA_Holife Vacuum Sealer, Silver Compact Food Saver Wet/Dry Vacuum Sealing System with Food Grade Starter Bags",
"amazonorderid" : "123-1239963-8862642",
"quantityshipped" : ,
"requestid" : "8cc6b5dc-f79e-4da4-b914-9f14388c0bbf",
"quantityordered" : ,
"orderitemid" : NumberLong("")
}
{
"_id" : ObjectId("590b35cd1d41c832ec3d2c03"),
"asin" : "B01M123ABC",
"sellersku" : "ABCEHM054AWUS-USAS2",
"title" : "wwwTEST_DATA_Holife Vacuum Sealer, Silver Compact Food Saver Wet/Dry Vacuum Sealing System with Food Grade Starter Bags",
"amazonorderid" : "123-1239963-8862642",
"quantityshipped" : ,
"requestid" : "8cc6b5dc-f79e-4da4-b914-9f14388c0bbf",
"quantityordered" : ,
"orderitemid" : NumberLong("")
}
> db.col.find().pretty()
{ "_id" : ObjectId("590b35877511f2683d345653"), "w" : }
{
"_id" : ObjectId("590b35ab1d41c832e2b6048b"),
"exception" : "<ns0:ListOrderItemsResponse xmlns:ns0=\"https://mws.amazonservices.com/Orders/2013-09-01\">\n <ns0:ListOrderItemsResult>\n <ns0:AmazonOrderId>123-1239963-8862642</ns0:AmazonOrderId>\n <ns0:OrderItems>\n <ns0:OrderItem>\n <ns0:ASIN>B01M123ABC</ns0:ASIN>\n <ns0:SellerSKU>ABCEHM054AWUS-USAS2</ns0:SellerSKU>\n <ns0:OrderItemId>12325810562154</ns0:OrderItemId>\n <ns0:Title>wwwTEST_DATA_Holife Vacuum Sealer, Silver Compact Food Saver Wet/Dry Vacuum Sealing System with Food Grade Starter Bags</ns0:Title>\n <ns0:QuantityOrdered>1</ns0:QuantityOrdered>\n <ns0:QuantityShipped>0</ns0:QuantityShipped>\n <ns0:PromotionIds />\n </ns0:OrderItem>\n </ns0:OrderItems>\n </ns0:ListOrderItemsResult>\n <ns0:ResponseMetadata>\n <ns0:RequestId>8cc6b5dc-f79e-4da4-b914-9f14388c0bbf</ns0:RequestId>\n </ns0:ResponseMetadata>\n</ns0:ListOrderItemsResponse>"
}
{
"_id" : ObjectId("590b35ab1d41c832e2b6048c"),
"asin" : "B01M123ABC",
"sellersku" : "ABCEHM054AWUS-USAS2",
"title" : "wwwTEST_DATA_Holife Vacuum Sealer, Silver Compact Food Saver Wet/Dry Vacuum Sealing System with Food Grade Starter Bags",
"amazonorderid" : "123-1239963-8862642",
"quantityshipped" : ,
"requestid" : "8cc6b5dc-f79e-4da4-b914-9f14388c0bbf",
"quantityordered" : ,
"orderitemid" : NumberLong("")
}
{
"_id" : ObjectId("590b35cd1d41c832ec3d2c03"),
"asin" : "B01M123ABC",
"sellersku" : "ABCEHM054AWUS-USAS2",
"title" : "wwwTEST_DATA_Holife Vacuum Sealer, Silver Compact Food Saver Wet/Dry Vacuum Sealing System with Food Grade Starter Bags",
"amazonorderid" : "123-1239963-8862642",
"quantityshipped" : ,
"requestid" : "8cc6b5dc-f79e-4da4-b914-9f14388c0bbf",
"quantityordered" : ,
"orderitemid" : NumberLong("")
}
{
"_id" : ObjectId("590b39841d41c833325a4dcd"),
"asin" : "B01M123ABC",
"sellersku" : "ABCEHM054AWUS-USAS2",
"title" : "wwwTEST_DATA_Holife Vacuum Sealer, Silver Compact Food Saver Wet/Dry Vacuum Sealing System with Food Grade Starter Bags",
"amazonorderid" : "123-1239963-8862642",
"quantityshipped" : ,
"requestid" : "8cc6b5dc-f79e-4da4-b914-9f14388c0bbf",
"quantityordered" : ,
"orderitemid" : NumberLong("")
}
{
"_id" : ObjectId("590b3cc51d41c83347fbfcb1"),
"asin" : "B01M123ABC",
"sellersku" : "ABCEHM054AWUS-USAS2",
"title" : "wwwTEST_DATA_Holife Vacuum Sealer, Silver Compact Food Saver Wet/Dry Vacuum Sealing System with Food Grade Starter Bags",
"amazonorderid" : "123-1239963-8862642",
"quantityshipped" : ,
"requestid" : "8cc6b5dc-f79e-4da4-b914-9f14388c0bbf",
"quantityordered" : ,
"orderitemid" : NumberLong("")
}
> db.col.find().count() > db.col.find().count() >
---->更高效的
nodeName,nodeValue未知 xml 入库方案
xml--->?--->database
json只是过程,不是目的;
想到一种算法是将xml转为string,然后借助正则辅助去处理该string,目测可行但是觉得似乎其不高效;;;;
也许xpath的原理就是上述算法??
SAX解析多层嵌套XML - donglindonglin的博客 - 博客频道 - CSDN.NET
http://blog.csdn.net/donglindonglin/article/details/51996926
wuser@ubuntu:~/apiamzpy$ python wl.py
<listiterator object at 0x7f6c99c20ed0>
[, , 'w1']
Traceback (most recent call last):
File "wl.py", line , in <module>
if t0.next():
StopIteration
wuser@ubuntu:~/apiamzpy$ vim wl.py l0 = [,,'w1']
t0 = l0.__iter__()
print t0
print list(t0)
if t0.next():
t0.next()
~
问题:
xml取出所有的nodeName、nodeValue对
0-不高效的方案:0-0-php / python 将xml处理为string;0-1-利用正则处理字符串。
w
http://effbot.org/zone/element-iterparse.htm
nodeName,nodeValue未知 xml 入库方案 The ElementTree iterparse Function的更多相关文章
- python中用ElementTree.iterparse()读取xml文件中的多层节点
我在使用Python解析比较大型的xml文件时,为了提高效率,决定使用iterparse()方法,但是发现根据网上的例子:每次if event == 'end':之后elem.clear()或者是每次 ...
- HTML DOM的nodeName,nodeValue,nodeType介绍
将HTML DOM中几个常用的属性做下介绍,工作中作为参考. nodeName 属性可依据节点的类型返回其名称. 元素节点的 nodeName 是标签名称 属性节点的 nodeName 是属性名称 文 ...
- HTML DOM 节点介绍(nodeName,nodeValue,nodeType)
对于初学者来说,HTML DOM 里面的 nodeName.nodeValue 以及 nodeType 三个属性的作用和取值不是很清楚.下面整理的信息包含有关于节点的详细信息,供参考. 节点信息 每个 ...
- HTML DOM nodeName nodeValue
在javascript在,我们得title在标签和文本,它们通常要求这样做 var obj =document.getElementsById("id1"); obj.nodeNa ...
- nodeName,nodeValue,nodeType,typeof 的区别
nodeName 属性含有某个节点的名称. 元素节点的 nodeName 是标签名称 属性节点的 nodeName 是属性名称 文本节点的 nodeName 永远是 #text 文档节 ...
- python解析xml文件时使用ElementTree和cElementTree的不同点;iter
在python中,解析xml文件时,会选用ElementTree或者cElementTree,那么两者有什么不同呢? 1.cElementTree速度上要比ElementTree快,比较cElemen ...
- XML解析方案
在iOS中,解析XML的手段有很多 苹果原生 NSXMLParser:SAX方式解析,使用简单 第三方框架 libxml2:纯C语言,默认包含在iOS SDK中,同时支持DOM和SAX方式解析 GDa ...
- 多级xml解析方案
package com.people.xmlToSql; import java.io.File; import java.io.IOException; import java.io.StringW ...
- mybatis的Mapper.xml文件SQL语句BadSqlGrammarException之FUNCTION错误系列
想必各位在开发过程中一定使用过:统计的功能,用到了很多SQL的函数,于是就直接写在Mapper文件中了: 比如: member_num,MAX(ID) AS newestLoanID,MIN (ID) ...
随机推荐
- Windows 运行时加载动态库
下面是一个运行时加载nvcuda.dll,并检测当前驱动版本最大支持的CUDA版本的例子. #include "cuda.h" #include <stdio.h> # ...
- 3.golang 的注释
package main import ( "fmt" "math" ) func main() { fmt.Println(pi(5000)) } // pi ...
- RabbitMQ几种队列模式
- 对Xcode菜单选项的详细探索(来自董铂然的微博http://www.cnblogs.com/dsxniubility/p/4983614.html)
本文调研Xcode的版本是 7.1,基本是探索了菜单的每一个按钮.虽然从xcode4一直用到了xcode7,但是一般都只是用了一些基础的功能,说来也惭愧.在一次偶然的机遇突然发现了“显示调用层级”的选 ...
- Nginx优化_数据包头部信息过大问题
如果客户端发出请求的URL头部信息过大,网站将不能及时响应,并通过状态码414报错. <center><h1>414 Request-URI Too Large</h1& ...
- 35C3 CTF - Web - php
参考链接 https://ctftime.org/task/7404 题目描述 PHP's unserialization mechanism can be exceptional. 解题流程 程序代 ...
- python笔记(3)---列表
lst = ["bob","jack","马云","马化腾","王健林"] del lst[1:3] ...
- java并发学习--第十章 java内存模型的内存语义
一.锁的内存语义 所为的java内存模型的内存语义指的就是在JVM中的实现原则. 锁的内存语义:锁除了让临界区互斥执行外,还可以让释放锁的线程向获取同一个锁的线程发送消息. 我们把上面这句话再整理下: ...
- php内置函数分析array_diff()
PHP_FUNCTION(array_diff) { zval *args; int argc, i; uint32_t num; HashTable exclude; zval *value; ze ...
- 使用Docker安装PaddlePaddle安装过程中出现的几个问题
Unable to create: 已停止该运行的命令,因为首选项变量"ErrorActionPreference"或通用参数设置为 Stop: 无法新建虚拟机. "Mo ...