Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205399

how to iterate through multiple elements in xml using R program

$
0
0

i am just new to R program and i am in the process of learning it. I have a sample xml file as below.

<?xml version="1.0" encoding="ISO-8859-1"?>    
<documents>
<Attribute ID="HCITM0014870" MultiValued="false" ProductMode="Normal" FullTextIndexed="true" 
ExternallyMaintained="false" Derived="false" Mandatory="false">
  <Validation BaseType="text" MinValue="" MaxValue="" MaxLength="" InputMask=""/>
  <DimensionLink DimensionID="Language"/>
  <MetaData>
    <Value AttributeID="Attribute-Group-Order">1</Value>
    <Value AttributeID="DisplaySequence">1</Value>
    <Value AttributeID="Attribute-Order">8</Value>
  </MetaData>
  <AttributeGroupLink AttributeGroupID="HCCHP0002088"/>
  <UserTypeLink UserTypeID="CNS"/>
  <UserTypeLink UserTypeID="PRD"/>
</Attribute>

<Attribute ID="GroupSEO" MultiValued="false" ProductMode="Property" FullTextIndexed="false" 
 ExternallyMaintained="false" Derived="false" Mandatory="false">
<Name>Group SEO Name</Name>
<Validation BaseType="text" MinValue="" MaxValue="" MaxLength="1024" InputMask=""/>
<DimensionLink DimensionID="Language"/>
<MetaData>
<Value AttributeID="Attribute-Group-Order">1</Value>
<Value AttributeID="Enterprise-Label">NAV-GR-SEONAME</Value>
<Value ID="#NAMED" AttributeID="Attribute-Group-Name">#NAMED</Value>
<Value AttributeID="Enterprise-Description">Navigation Group SEO Name</Value>
<Value AttributeID="Attribute-Order">3</Value>
</MetaData>
<AttributeGroupLink AttributeGroupID="HTCategorizationsNavigation"/>
<AttributeGroupLink AttributeGroupID="HTDigitalServicesModifyClassifications"/>
<UserTypeLink UserTypeID="ENT-Group"/>
<UserTypeLink UserTypeID="NAVGRP"/>
<UserTypeLink UserTypeID="ENT-SubCategory"/>
<UserTypeLink UserTypeID="ENT-Category"/>
</Attribute>
</documents>

i want convert this xml into a data frame in R,this is my following code that i wrote to convert into data frame.

library(XML)
data=htmlParse("E:/Project/Sample.xml")
print(data)
fulltextindexed=xpathSApply(data,"normalize-space(//attribute/@fulltextindexed)")
ID=xpathSApply(data,"normalize-space(//attribute/@id)")
multivalued=xpathSApply(data,"normalize-space(//attribute/@multivalued)")
productmode=xpathSApply(data,"normalize-space(//attribute/@productmode)")
externallymaintained=xpathSApply(data,"normalize-space(//attribute/@externallymaintained)")
derived=xpathSApply(data,"normalize-space(//attribute/@derived)")
mandatory=xpathSApply(data,"normalize-space(//attribute/@mandatory)")
attribute.group.order=xpathSApply(data,"//value[@attributeid='Attribute-Group-Order']",xmlValue)
enterprise.description=xpathSApply(data,"//value[@attributeid='Enterprise-Description']",xmlValue)
user.type.id=paste(xpathSApply(data,"//usertypelink/@usertypeid"),collapse = "|")
df=data.frame(ID,fulltextindexed,multivalued,productmode,externallymaintained,derived,mandatory,attribute.group.order,enterprise.description,user.type.id)
print(df)

this is the output of my code

          ID fulltextindexed multivalued productmode externallymaintained derived mandatory attribute.group.order    enterprise.description                                          user.type.id
          1 HCITM0014870            true       false      Normal                false   false     false                     1 Navigation Group SEO Name CNS|PRD|ENT-Group|NAVGRP|ENT-SubCategory|ENT-Category
          2 HCITM0014870            true       false      Normal                false   false     false                     1 Navigation Group SEO Name CNS|PRD|ENT-Group|NAVGRP|ENT-SubCategory|ENT-Category

but as you can see even though my xml has two elements,the output is coming twice but only the first element is coming twice.I want the second element also.I have searched everywhere but could not find a solution for this.If anyone could help me it would be of great help. Thanks in advance.


Viewing all articles
Browse latest Browse all 205399

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>