Advertisement
asimryu

read xml and convert to json

Jul 17th, 2020
842
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 4.54 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Document</title>
  6.     <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
  7.     <script src="http://code.jquery.com/jquery-3.5.0.min.js"></script>
  8. </head>
  9. <body>
  10.    <div class="container">
  11.        <h1>Food Menu</h1>
  12.        <table id="foodlist" class="table">
  13.            <thead class="thead-dark">
  14.                <th>메뉴</th>
  15.                <th>가격</th>
  16.                <th>설명</th>
  17.                <th>이미지</th>
  18.            </thead>
  19.            <tbody>              
  20.            </tbody>
  21.        </table>
  22.    </div>
  23.     <script>
  24.        
  25.         let foods_json = [];
  26.         let total = 0;
  27.        
  28.         const init = () => {
  29.             $.ajax({
  30.                 url: "sample.xml",
  31.                 tyle: "xml",
  32.                 success: xmlParser
  33.             });            
  34.         }
  35.        
  36.         const xmlParser = (xml) => {
  37.             total = $(xml).find("totalCnt")[0];
  38.             let foods = $(xml).find("food");
  39.             for(let i=0; i<foods.length; i++) {
  40.                let food = {};
  41.                food.name = $(foods[i]).find("name").text();
  42.                food.price = $(foods[i]).find("price").text();
  43.                food.imgs = [];
  44.                food.description = $(foods[i]).find("description").text();
  45.                let images = $(foods[i]).find("image");
  46.                for(let j=0; j<images.length; j++) {
  47.                    let img = $(images[j]).text();
  48.                    food.imgs.push(img);
  49.                }
  50.                foods_json.push(food);
  51.            }
  52.            display(foods_json);
  53.        }
  54.        
  55.        const display = (foods_json) => {
  56.             for(let i=0; i<foods_json.length; i++) {
  57.                let html = `
  58.                        <tr>
  59.                             <td>${foods_json[i].name}</td>
  60.                             <td>${foods_json[i].price}</td>
  61.                             <td>${foods_json[i].description}</td>
  62.                             <td>${foods_json[i].imgs}</td>
  63.                         </tr>
  64.                     `;
  65.                 $("#foodlist tbody").append(html);
  66.             }
  67.         }
  68.        
  69.         (() => {
  70.             init();
  71.         })();
  72.        
  73.     </script>
  74. </body>
  75. </html>
  76. <!--
  77. sample.xml
  78.  
  79. <?xml version="1.0" encoding="UTF-8"?>
  80. <food_menu>
  81.  <totalCnt>39</totalCnt>
  82.  <foods>
  83.      <food>
  84.        <name>김치찌개</name>
  85.        <price>8000</price>
  86.        <description>묵은지와 돼지고기가 잘 어우러진 담백한 김치찌개입니다.</description>
  87.        <images>
  88.            <image>food1_1.jpg</image>
  89.            <image>food1_2.jpg</image>
  90.            <image>food1_3.jpg</image>
  91.        </images>
  92.      </food>
  93.      <food>
  94.        <name>돼지국밥</name>
  95.        <price>7500</price>
  96.        <description>깊은 사골뼈 육수에 얇게 썬 돼지고기와 내장을 넣어 끓인 맛 있는 돼지 국밥입니다.</description>
  97.        <images>
  98.            <image>food2_1.jpg</image>
  99.            <image>food2_2.jpg</image>
  100.            <image>food2_3.jpg</image>
  101.        </images>
  102.      </food>
  103.      <food>
  104.        <name>황태국밥</name>
  105.        <price>9000</price>
  106.        <description>강원도 인제군 남면 용대리에서 겨우내 얼린 질 좋은 황태로 끓은 진한 국묵의 황태국빕입니다.</description>
  107.        <images>
  108.            <image>food3_1.jpg</image>
  109.            <image>food3_2.jpg</image>
  110.            <image>food3_3.jpg</image>
  111.        </images>
  112.      </food>
  113.      <food>
  114.        <name>새싹비빔밥</name>
  115.        <price>7000</price>
  116.        <description>각종 채소 새싹과 야채를 듬뿍 넣은 건강에 좋은 새싹 비밈밥입니다.</description>
  117.        <images>
  118.            <image>food4_1.jpg</image>
  119.            <image>food4_2.jpg</image>
  120.            <image>food4_3.jpg</image>
  121.        </images>
  122.      </food>
  123.      <food>
  124.        <name>뼈다귀해장국</name>
  125.        <price>8000</price>
  126.        <description>살이 많은 국내산 돼지 등뼈를 우거지와 푹 끓여 부드럽게 드실 수 있습니다.</description>
  127.        <images>
  128.            <image>food5_1.jpg</image>
  129.            <image>food5_2.jpg</image>
  130.            <image>food5_3.jpg</image>
  131.        </images>
  132.      </food>      
  133.  </foods>
  134. </food_menu>
  135.  
  136.  
  137. -->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement