javascriptangularjsxml-parsingxml-to-json

Parse the XML to get the Node Value


I get a XML response which is 16 MB data when calling an end point like below

<?xml version="1.0" encoding="UTF-8"?>
<edmx:Edmx Version="4.0" xmlns:edmx="http://example.org/edmx">
<edmx:DataServices>
    <Schema xmlns="http://example.org/edm" Namespace="PlatformForScience" Alias="pfs">
     <EntityType Name="VHP_BI_SAMPLE" BaseType="pfs.SAMPLE">
         <Property Name="JAX_SAMPLE_COMMENTS" Type="Edm.String"></Property>
         <NavigationProperty Name="SAMPLE_CMQREQUEST" Type="pfs.CMQ_REQUEST"
                             Partner="REV_SAMPLE_CMQREQUEST_VHP_BI_SAMPLE">
    </EntityType>

I need to parse my XML and look for the <EntityType Name="VHP_BI_SAMPLE" BaseType="pfs.SAMPLE"> and get the Partner associated with the NavigationProperty Name="SAMPLE_CMQREQUEST" . I am not sure how to parse the XML with Angular JS or convert them in to JSON so I can access the Property, how can I do this

I am not sure how I can install the X2JS package from github in to my AngularJS project


Solution

  • Somewhat modifying your xml to make it valid:

    <EntityType Name="VHP_BI_SAMPLE" BaseType="pfs.SAMPLE">
             <Property Name="JAX_SAMPLE_COMMENTS" Type="Edm.String">
             </Property>
             <NavigationProperty Name="SAMPLE_CMQREQUEST" Type="pfs.CMQ_REQUEST"
                                 Partner="REV_SAMPLE_CMQREQUEST_VHP_BI_SAMPLE">
            </NavigationProperty>
    </EntityType>
    

    The following xpath expression

    EntityType[@Name="VHP_BI_SAMPLE"][@BaseType="pfs.SAMPLE"]/descendant::NavigationProperty[@Name="SAMPLE_CMQREQUEST"]/@Partner
    

    will select

    REV_SAMPLE_CMQREQUEST_VHP_BI_SAMPLE

    I believe this is what you're looking for.