За последние 24 часа нас посетили 17690 программистов и 1709 роботов. Сейчас ищут 924 программиста ...

xml to json

Тема в разделе "Сделайте за меня", создана пользователем elfrom, 14 мар 2017.

  1. elfrom

    elfrom Новичок

    С нами с:
    14 сен 2016
    Сообщения:
    10
    Симпатии:
    0
    добрый день Уважаемые Гуру php, имеется xml файл
    в котором содержатся данные о работах (из программы планирования)
    <tasks>
    <task id="1" name="Проект" actual_start="18.05.2016 08:00" actual_end="08.06.2016 16:00" progress="0" level="1"/>
    <task id="2" name="Под проект" actual_start="18.05.2016 08:00" actual_end="08.06.2016 16:00" progress="0" level="1"/>
    <task id="3" name="Работа" actual_start="18.05.2016 08:00" actual_end="08.06.2016 16:00" progress="0" level="2"/>
    <task id="4" name="Под работа" actual_start="18.05.2016 08:00" actual_end="07.06.2016 10:07" progress="0" level="3"/>
    <task id="5" name="Перечень работ" actual_start="18.05.2016 08:00" actual_end="18.05.2016 11:11" progress="100" level="4"/>
    <task id="6" name="Перечень работ" actual_start="18.05.2016 11:11" actual_end="21.05.2016 10:13" progress="100" level="4"/>
    <task id="7" name="Перечень работ" actual_start="21.05.2016 10:13" actual_end="24.05.2016 12:20" progress="100" level="4"/>
    <task id="8" name="Перечень работ" actual_start="26.05.2016 12:20" actual_end="29.05.2016 14:17" progress="100" level="4"/>
    <task id="9" name="Перечень работ" actual_start="29.05.2016 14:17" actual_end="30.05.2016 16:00" progress="100" level="4"/>
    <task id="10" name="Перечень работ" actual_start="05.06.2016 11:15" actual_end="07.06.2016 10:07" progress="100" level="4"/>
    <task id="11" name="Под работа" actual_start="21.05.2016 10:13" actual_end="26.05.2016 10:13" progress="0" level="3"/>
    <task id="12" name="Перечень работ" actual_start="21.05.2016 10:13" actual_end="26.05.2016 10:13" progress="100" level="4"/>
    <task id="13" name="Под работа" actual_start="24.05.2016 12:20" actual_end="02.06.2016 10:31" progress="0" level="3"/>
    <task id="14" name="Перечень работ" actual_start="24.05.2016 12:20" actual_end="29.05.2016 08:58" progress="100" level="4"/>
    <task id="15" name="Перечень работ" actual_start="24.05.2016 12:20" actual_end="02.06.2016 10:31" progress="100" level="4"/>
    <task id="16" name="Под работа" actual_start="25.05.2016 08:00" actual_end="08.06.2016 16:00" progress="0" level="3"/>
    <task id="17" name="Перечень работ" actual_start="25.05.2016 08:00" actual_end="25.05.2016 14:37" progress="100" level="4"/>
    <task id="18" name="Перечень работ" actual_start="25.05.2016 14:37" actual_end="31.05.2016 12:30" progress="100" level="4"/>
    <task id="19" name="Перечень работ" actual_start="31.05.2016 12:30" actual_end="02.06.2016 08:08" progress="100" level="4"/>
    <task id="20" name="Перечень работ" actual_start="05.06.2016 11:15" actual_end="08.06.2016 09:30" progress="100" level="4"/>
    <task id="21" name="Перечень работ" actual_start="08.06.2016 09:30" actual_end="08.06.2016 16:00" progress="100" level="4"/>
    </tasks>

    а так же информация о связях (последовательности работ)

    <connectors>
    <connector type="FinishStart" from="5" to="6"/>
    <connector type="FinishStart" from="6" to="7"/>
    <connector type="FinishStart" from="7" to="8"/>
    <connector type="FinishStart" from="8" to="9"/>
    <connector type="FinishStart" from="9" to="10"/>
    <connector type="FinishStart" from="6" to="12"/>
    <connector type="FinishStart" from="7" to="14"/>
    <connector type="StartStart" from="14" to="15"/>
    <connector type="FinishStart" from="15" to="10"/>
    <connector type="FinishStart" from="17" to="18"/>
    <connector type="FinishStart" from="18" to="19"/>
    <connector type="FinishStart" from="20" to="21"/>
    <connector type="FinishStart" from="9" to="20"/>
    </connectors>



    руками получается такой вид

    где из XML
    [XML]id= "id"
    [XML]name= "Name"
    [XML]actual_start="StartDate"
    [XML]actual_end="EndDate"
    [XML]progress="PercentDone"

    и если это "children" то подпись "expanded": true, а если она не "Children", то "leaf": true

    ------------------------------------------------JSON------------------------------------

    "tasks" : {
    "rows" : [
    {
    "id":1,
    "StartDate":"2017-03-10",
    "EndDate":"2017-05-04",
    "Name":"Проект",
    "PercentDone":10,
    "expanded": true,
    "children":[
    {
    "id":2,
    "StartDate":"2017-03-10",
    "EndDate":"2017-05-04",
    "Name":"Фаза",
    "PercentDone":10,
    "expanded": true,
    "children":[
    {
    "id":3,
    "StartDate":"2017-03-10",
    "EndDate":"2017-05-04",
    "Name":"Фаза 2",
    "PercentDone":10,
    "expanded": true,
    "children":[
    {
    "id":4,
    "StartDate":"2017-03-10",
    "EndDate":"2017-04-19",
    "Name":"Фаза 3",
    "PercentDone":10,
    "expanded": true,
    "children":[
    {
    "id":5,
    "StartDate":"2017-03-10",
    "EndDate":"2017-03-16",
    "Name":"Работа",
    "PercentDone":10,
    "leaf": true
    },
    {
    "id":6,
    "StartDate":"2017-03-24",
    "EndDate":"2017-03-30",
    "Name":"Работа",
    "PercentDone":10,
    "leaf": true
    },
    {
    "id":7,
    "StartDate":"2017-04-06",
    "EndDate":"2017-04-12",
    "Name":"Работа",
    "PercentDone":10,
    "leaf": true
    },
    {
    "id":8,
    "StartDate":"2017-04-13",
    "EndDate":"2017-04-19",
    "Name":"Работа",
    "PercentDone":10,
    "leaf": true
    },
    {
    "id":9,
    "StartDate":"2017-03-17",
    "EndDate":"2017-03-23",
    "Name":"Работа",
    "PercentDone":10,
    "leaf": true
    },
    {
    "id":10,
    "StartDate":"2017-03-24",
    "EndDate":"2017-03-30",
    "Name":"Работа",
    "PercentDone":10,
    "leaf": true
    }]},
    {
    "id":11,
    "StartDate":"2017-04-27",
    "EndDate":"2017-05-04",
    "Name":"Фаза 3",
    "PercentDone":0,
    "expanded": true,
    "children":[
    {
    "id":12,
    "StartDate":"2017-04-27",
    "EndDate":"2017-05-04",
    "Name":"Работа",
    "PercentDone":0,
    "leaf": true
    }]}]},
    {
    "id":13,
    "StartDate":"2017-03-31",
    "EndDate":"2017-04-26",
    "Name":"Фаза 5",
    "PercentDone":0,
    "expanded": true,
    "children":[
    {
    "id":14,
    "StartDate":"2017-04-20",
    "EndDate":"2017-04-26",
    "Name":"Работа",
    "PercentDone":0,
    "leaf": true
    },
    {
    "id":15,
    "StartDate":"2017-03-31",
    "EndDate":"2017-04-05",
    "Name":"Работа",
    "PercentDone":0,
    "leaf": true
    }]}]}]}]}}


    а связи преобразую в такой вид

    {
    "success" : true,

    "dependencies" : {
    "rows" : [
    {"Id" : 1, "From" : 5, "To" : 6, "Type" : 2},
    {"Id" : 2, "From" : 6, "To" : 7, "Type" : 2},
    {"Id" : 3, "From" : 7, "To" : 8, "Type" : 2},
    {"Id" : 4, "From" : 8, "To" : 9, "Type" : 2},
    {"Id" : 5, "From" : 9, "To" : 10, "Type" : 2},
    {"Id" : 6, "From" : 6, "To" : 12, "Type" : 2},
    {"Id" : 7, "From" : 7, "To" : 14, "Type" : 2},
    {"Id" : 8, "From" : 14, "To" : 15, "Type" : 1},
    {"Id" : 9, "From" : 15, "To" : 10, "Type" : 2},
    {"Id" : 10, "From" : 17, "To" : 18, "Type" : 2},
    {"Id" : 11, "From" : 18, "To" : 19, "Type" : 2},
    {"Id" : 12, "From" : 20, "To" : 21, "Type" : 2},
    {"Id" : 13, "From" : 9, "To" : 20, "Type" : 2}
    ]
    }}
    где

    "id" номер по порядку (просто счетчик n+1)
    [из XML]
    <connector type="FinishStart" равно "Type" : 2
    а <connector type="StartStart" "Type" : 1


    я так полагаю там должен быть вид чегото такого, но как быть с children?
    PHP:
    1. foreach($items as $item) {
    2.   $r = new Task();
    3.  
    4.   // rows
    5.   $r->id = $item['id'];
    6.   $r->text = $item['name'];
    7.   $r->actual_start = $item['StartDate'];
    8.   $r->actual_end = $item['EndDate'];
    9.   $r->progress = $item['PercentDone'];
    10.  
    11.   $result[] = $r;
     

    Вложения:

    • XML.zip
      Размер файла:
      1,2 КБ
      Просмотров:
      2
    • JSON.zip
      Размер файла:
      914 байт
      Просмотров:
      3
    #1 elfrom, 14 мар 2017
    Последнее редактирование модератором: 14 мар 2017
  2. ADSoft

    ADSoft Старожил

    С нами с:
    12 мар 2007
    Сообщения:
    3.866
    Симпатии:
    753
    Адрес:
    Татарстан
    имхо или children излишне, либо таблица связей .... ибо имеют один и тот-же смысл
     
  3. elfrom

    elfrom Новичок

    С нами с:
    14 сен 2016
    Сообщения:
    10
    Симпатии:
    0
    к сожалению нет, children выступает в роли создания иерархической структуры т.е. указывает какие работы входят в тот или иной уровень, а таблица связей указывает какие работы (последовательность выполнения) за какой выполняются
     
  4. ADSoft

    ADSoft Старожил

    С нами с:
    12 мар 2007
    Сообщения:
    3.866
    Симпатии:
    753
    Адрес:
    Татарстан
    тогда что-то типа такого будет
    Код (Text):
    1. $r->children = children($item['id'],$item['level'];)
    где children - рекурсивная функция которая готовит то что вам нужно. Так как нет привязки детей к родителям явной - то нужно использовать level
    функцию думаю сами напишите
     
  5. elfrom

    elfrom Новичок

    С нами с:
    14 сен 2016
    Сообщения:
    10
    Симпатии:
    0
    к сожалению нет(