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动态数据入库,   解决方案:  
 
入库结果
 
test-data
 
 

---->更高效的

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的更多相关文章

  1. python中用ElementTree.iterparse()读取xml文件中的多层节点

    我在使用Python解析比较大型的xml文件时,为了提高效率,决定使用iterparse()方法,但是发现根据网上的例子:每次if event == 'end':之后elem.clear()或者是每次 ...

  2. HTML DOM的nodeName,nodeValue,nodeType介绍

    将HTML DOM中几个常用的属性做下介绍,工作中作为参考. nodeName 属性可依据节点的类型返回其名称. 元素节点的 nodeName 是标签名称 属性节点的 nodeName 是属性名称 文 ...

  3. HTML DOM 节点介绍(nodeName,nodeValue,nodeType)

    对于初学者来说,HTML DOM 里面的 nodeName.nodeValue 以及 nodeType 三个属性的作用和取值不是很清楚.下面整理的信息包含有关于节点的详细信息,供参考. 节点信息 每个 ...

  4. HTML DOM nodeName nodeValue

    在javascript在,我们得title在标签和文本,它们通常要求这样做 var obj =document.getElementsById("id1"); obj.nodeNa ...

  5. nodeName,nodeValue,nodeType,typeof 的区别

        nodeName 属性含有某个节点的名称.  元素节点的 nodeName 是标签名称  属性节点的 nodeName 是属性名称  文本节点的 nodeName 永远是 #text  文档节 ...

  6. python解析xml文件时使用ElementTree和cElementTree的不同点;iter

    在python中,解析xml文件时,会选用ElementTree或者cElementTree,那么两者有什么不同呢? 1.cElementTree速度上要比ElementTree快,比较cElemen ...

  7. XML解析方案

    在iOS中,解析XML的手段有很多 苹果原生 NSXMLParser:SAX方式解析,使用简单 第三方框架 libxml2:纯C语言,默认包含在iOS SDK中,同时支持DOM和SAX方式解析 GDa ...

  8. 多级xml解析方案

    package com.people.xmlToSql; import java.io.File; import java.io.IOException; import java.io.StringW ...

  9. mybatis的Mapper.xml文件SQL语句BadSqlGrammarException之FUNCTION错误系列

    想必各位在开发过程中一定使用过:统计的功能,用到了很多SQL的函数,于是就直接写在Mapper文件中了: 比如: member_num,MAX(ID) AS newestLoanID,MIN (ID) ...

随机推荐

  1. Nginx 入门了解

    Nginx的产生 没有听过Nginx?那么一定听过它的"同行"Apache吧!Nginx同Apache一样都是一种WEB服务器.基于REST架构风格,以统一资源描述符(Unifor ...

  2. CQRS框架(nodejs的DDD开发落地框架)初识感想

    CQRS是啥?DDD又是啥? 这两个概念其实没什么神秘的,当然此文章中的这两个概念以曾老师的课程为准(关于CQRS和DDD的标准概念,google上已经很多了,不再赘述.) DDD(Domain Dr ...

  3. k3 cloud中员工离职以后释放站点

    进入金蝶云企业平台:https://cloud.kingdee.com/qy,找到对应的用户并选择离职 给替代的员工加上专业应用组: 客户端也加上分组并且同步注册用户 管理中心更新用户许可:

  4. linux 打包和压缩的概念和区别

    对于刚刚接触Linux的人来说,一定会给Linux下一大堆各式各样的文件名 给搞晕.别个不说,单单就压缩文件为例,我们知道在Windows下最常见的压缩文件就只有两种,一是,zip,另一个是.rar. ...

  5. VLAN原理详解[转载] 网桥--交换机---路由器

    来自:http://blog.csdn.net/phunxm/article/details/9498829 一.什么是桥接          桥接工作在OSI网络参考模型的第二层数据链路层,是一种以 ...

  6. Linux学习--第三天--linux文件目录、ls、mkdir、mv、rm、touch、cat、tac、more、less、head、tail、ln、chmod、chown、chgrp、umask

    文件目录 目录名 备注 bin 下面的命令所有人都可以运行 sbin 只有root才能运行,s代表super /mnt,/media,/misc 都是挂载目录,但一般只用mnt /opt 第三方软件安 ...

  7. 基于FPGA的以太网开发

    基于FPGA的以太网开发,在调试过的FPGA玩家开来,其实算不上很难的技术!但是如果只是菜鸟级别的选手,没有调试过的话,就有些头疼了!早在自己在实习的时候,就接触到XAUI(万兆以太网口)接口,但是由 ...

  8. Linux架构之Nginx 常见问题

    第54章 Nginx常见问题 一.Nginx多Sever优先级 在开始处理一个http请求时,nginx会取出header头中的Host变量,与nginx.conf中每个server的server_n ...

  9. 洛谷P1879 [USACO06NOV]玉米田Corn Fields (状态压缩DP)

    题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ...

  10. RMQ 区间最大值最小值 最频繁次数

    区间的最大值和最小值 #include <cstdio> #include <cstring> #include <cmath> #include <iost ...