Mongodb 使用Groovy将XML转换为GeoJson

Mongodb 使用Groovy将XML转换为GeoJson,mongodb,groovy,geojson,Mongodb,Groovy,Geojson,我一直在尝试使用这个伟大的工具来读取XML文件,将它们转换为GeoJson,并最终将它们导入MongoDB 我的示例XML文件如下所示: <AName att1="sequence" att2="xx" att3="xxx"> <Loc1>0</Loc1> <Loc2>0</Loc2> </AName> <AName att1="sequence" att2="xx" att3="xxx">

我一直在尝试使用这个伟大的工具来读取XML文件,将它们转换为GeoJson,并最终将它们导入MongoDB

我的示例XML文件如下所示:

<AName att1="sequence" att2="xx" att3="xxx">
    <Loc1>0</Loc1>
    <Loc2>0</Loc2>
</AName> 

<AName att1="sequence" att2="xx" att3="xxx">
    <Loc1>3</Loc1>
    <Loc2>6</Loc2>
</AName> 
我已经开始使用Groovy(我以前从未使用过),但我不确定实际的GeoJson创建是否与我的需求相似(我实际上认为它不起作用)

虽然已创建索引,但在我的集合中看不到任何记录。我的代码中是否有明显的错误?映射是否在多边形中递归添加坐标数组?有没有办法更好地排除故障?(我目前正在使用mongo-java-driver-3.2.1)

非常感谢你的帮助

所以,我想到了这个:

import groovy.json.*

def xml = '''
<root>
<AName att1="sequence" att2="xx" att3="xxx">
    <Loc1>0</Loc1>
    <Loc2>0</Loc2>
</AName> 

<AName att1="sequence" att2="xx" att3="xxx">
    <Loc1>3</Loc1>
    <Loc2>6</Loc2>
</AName> 
</root>
'''

def doc = new XmlSlurper().parseText(xml)

def polymap = [
    type: 'Polygon',
    name: doc.AName.head().@att1.text(),
    points: doc.AName.collect { [it.Loc1.text() as Double, it.Loc2.text() as Double] } 
]

def json = new JsonBuilder(polymap).toString()

assert json == '{"type":"Polygon","name":"sequence","points":[[0.0,0.0],[3.0,6.0]]}'
import groovy.json*
def xml=''
0
0
3.
6.
'''
def doc=new XmlSlurper().parseText(xml)
def polymap=[
键入:“多边形”,
名称:doc.AName.head()。@att1.text(),
要点:doc.AName.collect{[it.Loc1.text()为双精度,it.Loc2.text()为双精度]}
]
def json=new JsonBuilder(polymap).toString()
断言json=='{“类型”:“多边形”,“名称”:“序列”,“点”:[[0.0,0.0],[3.0,6.0]]}'

你想要什么(我想)

你能发布一些实际的xml,以及你期望它产生的实际json吗?@time_-yates,我已经澄清了我想要实现的GeoJson类型。希望这有帮助。但是XML中的坐标在哪里?X属性在哪里?您发送的示例xml与预期的输出没有相似之处,或者codeNow应该更好一些,请看一看。另请参考我获取代码的示例(链接):感谢您的帮助,非常感谢您的解决方案确实有效,我只需将.head()替换为.find()。我想知道是否有更好的方法来读取整个xml。我可以做一些像def file=new-XmlSlurper().parse(new-file('C:\\file.xml'))这样的事情,但是如何解析xml的整个内容呢?谢谢,我。我已经阅读了前面提到的整个文件,并通过它来构建地理结构和所有漂亮的作品。而不是collection.insert(新的BasicDBObject(polymap));我只是使用了collection.insertOne(新文档(polymap));然后在这里创建了2dspehere。
    //Here I am using the XmlSlurper() to parse the xml file

    def file=  new XmlSlurper().parse(new File('MyFile.xml'));
    def a = file.AName[0].@att1;
    println("AName " + a);
    def loc1= file.depthFirst().find() {it.name() == 'Loc1'};
    def loc2= file.depthFirst().find()  {it.name() == 'Loc2'};

   //Here I am converting the Java representation of my XML file to a GeoJson format

file.node.each {child ->
    Map myMap= [type: 'Polygon',
        name : a,
        loc: [coordinates: 
               [Double.valueOf(loc1),  Double.valueOf(loc2)],
               ]]

  //Finally I am inserting the BasicDBObject and creating the 2dsphere index

  collection.insert(new BasicDBObject(myMap));
  collection.createIndex(new BasicDBObject('loc', '2dsphere'));
import groovy.json.*

def xml = '''
<root>
<AName att1="sequence" att2="xx" att3="xxx">
    <Loc1>0</Loc1>
    <Loc2>0</Loc2>
</AName> 

<AName att1="sequence" att2="xx" att3="xxx">
    <Loc1>3</Loc1>
    <Loc2>6</Loc2>
</AName> 
</root>
'''

def doc = new XmlSlurper().parseText(xml)

def polymap = [
    type: 'Polygon',
    name: doc.AName.head().@att1.text(),
    points: doc.AName.collect { [it.Loc1.text() as Double, it.Loc2.text() as Double] } 
]

def json = new JsonBuilder(polymap).toString()

assert json == '{"type":"Polygon","name":"sequence","points":[[0.0,0.0],[3.0,6.0]]}'