Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to use Treeview directive from AngularJS. The stored procedure is returning xml.The tree view directive takes json format. The Controller will get the data from service.I am stuck trying to convert xml to json in service.

Following is the xml structure:

<Company Data="New Company">
  <Manager Data="Working">
    <Employee Data="ABC" />
    <Employee Data="DEF" />
    <Employee Data="GHI">
      <SubEmployee Data="Approval">
        <Stuff Data="Financial" />
        <Stuff Data="Consol" />
      </SubEmployee>
      <SubEmployee Data="Rolled-Over">
        <Stuff Data="Corporate" />
      </SubEmployee>
    </Employee>
  </Manager>
</Company>

Below is the expected JSON :

[
  {
    label: "New Company",
    id: "Company",
    children: [
      {
        label: "Working",
        id: "Manager",
        children: [
          {
            label: "ABC",
            id: "Employee",
            children: [

            ]
          },
          {
            label: "DEF",
            id: "Employee",
            children: [

            ]
          },
          {
            label: "GHI",
            id: "Employee",
            children: [
              {
                label: "Approval",
                id: "SubEmployee",
                children: [
                  {
                    label: "Financial",
                    id: "Stuff",
                    children: [

                    ]
                  },
                  {
                    label: "Consol",
                    id: "Stuff",
                    children: [

                    ]
                  }
                ]
              },
              {
                label: "RolledOver",
                id: "SubEmployee",
                children: [
                  {
                    label: "Corporate",
                    id: "Stuff",
                    children: [

                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
share|improve this question
    
Are you asking how to convert XML to JSON using javascript? –  rdjs Dec 18 '13 at 17:26
    
I want to convert the xml to the specified Json format. –  user3083626 Dec 19 '13 at 3:34
add comment

1 Answer

You have two choices.

  1. Return the data from the API in the JSON format you require
  2. Convert the XML to JSON in your angular application using javascript.

I would recommend option 1 if that is possible. For option 2 this SO answer is a good for XML/JSON conversion in javascript:

XML <-> JSON conversion in Javascript

If you read the answers on the above link you will see why option 1 is preferable. Converting between these formats can be problematic.

share|improve this answer
    
Thanks a lot.I am getting the xml from .edmx. The stored procedure is returning the xml. Yes, The second option is complicated.Should i convert the xml to json in the .cs file, which is calling the stored procedure?What do you suggest? –  user3083626 Dec 20 '13 at 5:32
    
I don't know anything about .edmx files. It looks like you are using c#. Here is a SO answer for xml to json in c# stackoverflow.com/questions/814001/… –  rdjs Dec 20 '13 at 7:45
    
Thanks. I tried to implement. Json , AngularJS are new to me.It is using static class JsonConvert to convert from xml to Json. So it is under which namespace.Which Directive or assembly reference do i need to add? –  user3083626 Dec 20 '13 at 8:11
    
That question would be better asked on the question I linked to. –  rdjs Dec 20 '13 at 8:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.