如何将具有父属性的XML属性解析为R中的数据帧

时间:2019-02-11 13:48:14

标签: r xml xpath

我正在尝试解析XML文件的节点和属性。在文件中,有一组具有属性的节点。嵌套的XML结构类似于数据框,但我想将其解析为数据框。

这是一个示例文件:

<?xml version="1.0" encoding="UTF-8"?>
<TrackMate version="3.8.0">
  <Model spatialunits="µm" timeunits="sec">
    <AllTracks>
      <Track name="Track_2" TRACK_ID="2" NUMBER_SPOTS="140" NUMBER_GAPS="0" >
        <Edge SPOT_SOURCE_ID="960769" SPOT_TARGET_ID="960778" LINK_COST="0.08756957830926632" />
        <Edge SPOT_SOURCE_ID="958304" SPOT_TARGET_ID="958308" LINK_COST="1.4003359672950089" />
        <Edge SPOT_SOURCE_ID="958316" SPOT_TARGET_ID="958322" LINK_COST="1.6985623204008202" />
      </Track>
      <Track name="Track_145" TRACK_ID="145" NUMBER_SPOTS="141" NUMBER_GAPS="0" >
        <Edge SPOT_SOURCE_ID="961623" SPOT_TARGET_ID="961628" LINK_COST="2.2678642015413755" />
        <Edge SPOT_SOURCE_ID="962122" SPOT_TARGET_ID="962127" LINK_COST="38.20777704254654" />
        <Edge SPOT_SOURCE_ID="961869" SPOT_TARGET_ID="961873" LINK_COST="0.2895609647324684" />
      </Track>
    </AllTracks>
  </Model>
</TrackMate>

我想创建一个具有边的所有属性和父级的TRACK_ID属性的数据框。我可以使用以下命令轻松创建具有所有边缘属性的数据框:

edges = data.frame(t(data.frame(xml_attrs(xml_find_all(xmlDoc, xpath = paste0('/TrackMate/Model/AllTracks//Edge'))))))
row.names(edges) = NULL

但是相应的轨道ID丢失了。我可以使用for循环解决此问题,但这通常不是“ R方式”。我想知道是否有更简单的解决方案? (例如,使用xpath查询)。

因此,最终所需的输出将是以下数据帧: output data frame

编辑:这更接近了,但随后在列表中混合了“跟踪”节点和“边缘”节点。

xml_find_all(xmlDoc, xpath = paste0('/TrackMate/Model/AllTracks//Edge | /TrackMate/Model/AllTracks/Track'))

1 个答案:

答案 0 :(得分:3)

“诀窍”是获取边缘节点的所有列表,并从此处使用override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) when (requestCode) { CameraCode -> if (resultCode == Activity.RESULT_OK) { var bitmap = data?.extras?.get("data") image.setImageBitmap(bitmap as Bitmap) stream = ByteArrayOutputStream() bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream) ByteImage = stream.toByteArray() _data.Image = ByteImage // if(_data.Image!==null) // { // Log.e("1","Ok") // } } GalleryCode -> if (resultCode == Activity.RESULT_OK) { var Uri = data!!.data var bitmap = MediaStore.Images.Media.getBitmap(activity.contentResolver, Uri) stream = ByteArrayOutputStream() image.setImageBitmap(bitmap as Bitmap) bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream) ByteImage = stream.toByteArray() _data.Image = ByteImage } } } ...您可以使用{{1}从每个边缘节点中选择Trach节点}来自 override fun onBindViewHolder(holder: customViewHolder?, position: Int) { var position = HomeList.get(position) var bitmap = BitmapFactory.decodeByteArray(position.Image,0,position.Image!!.size) holder?.text?.text = position.text holder?.image?.setImageBitmap(bitmap) holder!!.l1.setOnClickListener { Toast.makeText(ctx,"${position.text}",Toast.LENGTH_SHORT).show() } } inner class customViewHolder(view:View) : RecyclerView.ViewHolder(view) { var image = view.findViewById<ImageView>(R.id.recycler_image) var text = view.findViewById<TextView>(R.id.recycler_title) var l1 = view.findViewById<RelativeLayout>(R.id.home_layout) }

已使用的库

xpath

样本数据

ancestor

代码

xpath

输出

#load libraries
library( xml2 )
library( magrittr )