0

I want to retrieve the XML value "Business Object" using R

The XML File is a yEd Diagram[1]. The file is shown below

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:java="http://www.yworks.com/xml/yfiles-common/1.0/java" xmlns:sys="http://www.yworks.com/xml/yfiles-common/markup/primitives/2.0" xmlns:x="http://www.yworks.com/xml/yfiles-common/markup/2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:y="http://www.yworks.com/xml/graphml" xmlns:yed="http://www.yworks.com/xml/yed/3" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://www.yworks.com/xml/schema/graphml/1.1/ygraphml.xsd">
  <!--Created by yEd 3.14.2-->
  <key attr.name="Description" attr.type="string" for="graph" id="d0"/>
  <key for="port" id="d1" yfiles.type="portgraphics"/>
  <key for="port" id="d2" yfiles.type="portgeometry"/>
  <key for="port" id="d3" yfiles.type="portuserdata"/>
  <key attr.name="url" attr.type="string" for="node" id="d4"/>
  <key attr.name="description" attr.type="string" for="node" id="d5"/>
  <key for="node" id="d6" yfiles.type="nodegraphics"/>
  <key for="graphml" id="d7" yfiles.type="resources"/>
  <key attr.name="url" attr.type="string" for="edge" id="d8"/>
  <key attr.name="description" attr.type="string" for="edge" id="d9"/>
  <key for="edge" id="d10" yfiles.type="edgegraphics"/>
  <graph edgedefault="directed" id="G">
    <data key="d0"/>
    <node id="n0">
      <data key="d5"/>
      <data key="d6">
        <y:GenericNode configuration="com.yworks.bpmn.Artifact.withShadow">
          <y:Geometry height="55.0" width="35.0" x="282.5" y="152.5"/>
          <y:Fill color="#FFFFFFE6" transparent="false"/>
          <y:BorderStyle color="#000000" type="line" width="1.0"/>
          <y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="12" fontStyle="plain" hasBackgroundColor="false" hasLineColor="false" height="18.701171875" modelName="custom" textColor="#000000" visible="true" width="90.70703125" x="-27.853515625" y="-22.701171875">Business Object<y:LabelModel>
              <y:SmartNodeLabelModel distance="4.0"/>
            </y:LabelModel>
            <y:ModelParameter>
              <y:SmartNodeLabelModelParameter labelRatioX="0.0" labelRatioY="0.5" nodeRatioX="0.0" nodeRatioY="-0.5" offsetX="0.0" offsetY="-4.0" upX="0.0" upY="-1.0"/>
            </y:ModelParameter>
          </y:NodeLabel>
          <y:StyleProperties>
            <y:Property class="java.awt.Color" name="com.yworks.bpmn.icon.line.color" value="#000000"/>
            <y:Property class="java.awt.Color" name="com.yworks.bpmn.icon.fill2" value="#d4d4d4cc"/>
            <y:Property class="java.awt.Color" name="com.yworks.bpmn.icon.fill" value="#ffffffe6"/>
            <y:Property class="com.yworks.yfiles.bpmn.view.BPMNTypeEnum" name="com.yworks.bpmn.type" value="ARTIFACT_TYPE_DATA_OBJECT"/>
            <y:Property class="com.yworks.yfiles.bpmn.view.DataObjectTypeEnum" name="com.yworks.bpmn.dataObjectType" value="DATA_OBJECT_TYPE_PLAIN"/>
          </y:StyleProperties>
        </y:GenericNode>
      </data>
    </node>
  </graph>
  <data key="d7">
    <y:Resources/>
  </data>
</graphml>

Using xmlSpy I can use the the xpath expression //y:NodeLabel to retrieve the result Business Object!

The R code is the following

# load libraries
library(XML)

path = "./Data/"
# Read yed XML file
file.names <- dir(path, pattern ="SingleBO.graphml")
data <- xmlParse(paste(path, file.names[1], sep=""))

# xpath expression
data[["//y:NodeLabel"]]

The result of the line data[["//y:NodeLabel"]] is shown below

<y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="12" fontStyle="plain" hasBackgroundColor="false" hasLineColor="false" height="18.701171875" modelName="custom" textColor="#000000" visible="true" width="90.70703125" x="-27.853515625" y="-22.701171875">Business Object<y:LabelModel><y:SmartNodeLabelModel distance="4.0"/></y:LabelModel>
        <y:ModelParameter><y:SmartNodeLabelModelParameter labelRatioX="0.0" labelRatioY="0.5" nodeRatioX="0.0" nodeRatioY="-0.5" offsetX="0.0" offsetY="-4.0" upX="0.0" upY="-1.0"/></y:ModelParameter>
</y:NodeLabel> 

What do I need to do in order to retrieve the result Business Object?

All help very much appreciated. John

[1] http://www.yworks.com/en/products_yed_download.html

1
  • 1
    @jonsinclair, this xml is too long. You should think about copying a simple XML example that still conveys the essential part of your problem. Commented Jul 21, 2015 at 19:39

2 Answers 2

2

The easiest thing would just be to use xmlValue()

xmlValue(data[["//y:NodeLabel"]])
# [1] "Business Object\n            \n          "

this does preserve some whitespace and newlines which may be undesireable. You can set trim=TRUE to clean up the selectin

xmlValue(data[["//y:NodeLabel"]], trim=TRUE)
# [1] "Business Object"
Sign up to request clarification or add additional context in comments.

Comments

1

Alternately, with xml2:

library(xml2) # i use the github version  
library(stringr)

dat <- read_xml(YOURXMLFILE)

str_trim(xml_text(xml_find_all(dat, "//y:NodeLabel", xml_ns(dat))))
## [1] "Business Object"

If you are a pipe-r:

library(magrittr)
library(xml2)
library(stringr)

dat <- read_xml(YOURXMLFILE)

dat %>% 
  xml_find_all("//y:NodeLabel", xml_ns(dat)) %>% 
  xml_text %>% 
  str_trim
## [1] "Business Object"

A test on a larger file:

dat <- read_xml("http://docs.yworks.com/graphml/demo/yext/graphml/resources/custom/demo.graphml")
dat %>% 
  xml_find_all("//y:NodeLabel", xml_ns(dat)) %>% 
  xml_text %>%
  str_trim
## [1] "1" "2" "3"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.