<p>如果要将数据导入到新数据库中,可能需要尝试导入工具:<a href="https://neo4j.com/docs/operations-manual/current/#import-tool" rel="nofollow">https://neo4j.com/docs/operations-manual/current/#import-tool</a></p>
<p>在这种情况下,您应该像以前一样解析XML文件,但是不要使用py2neo将数据插入Neo4j,只需编写一个CSV文件,然后调用导入工具。在</p>
<p>请参见下面的一种可能的方法:</p>
<pre><code>import csv
from xml.dom import minidom
def getAttribute(node,attribute,default=None):
attr = node.getElementsByTagName(attribute)[0]
return attr.firstChild.data if attr.firstChild else default
xml_doc = minidom.parse(open("users.xml"))
persons = xml_doc.getElementsByTagName('user')
users = []
attrs = ['name','screen_name','location','description','profile_image_url','friends_count','url']
mapping = {'user_id': 'user_id:ID(User)',
'name': 'name:string',
'screen_name': 'screen_name:string',
'location': 'location:string',
'description': 'description:string',
'profile_image_url': 'profile_image_url:string',
'friends_count': 'friends_count:int',
'url': 'url:string'}
with open('users.csv','w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=mapping.values())
writer.writeheader()
for person in persons:
user = {mapping[attr]: getAttribute(person, attr) for attr in attrs}
user[mapping['user_id']] = getAttribute(person, 'id')
writer.writerow(user)
</code></pre>
<p>将xml转换为csv文件后,请运行导入工具:</p>
^{pr2}$
<p>我想您还需要创建节点之间的关系(?)。您应该阅读导入工具文档,并为节点和关系调用带有csv文件的导入工具</p>