Как очистить XML-ответ в узле? - PullRequest
0 голосов
/ 17 октября 2019

У меня есть XML-файл, извлеченный из API, который я хотел бы очистить и преобразовать в JSON. Как я могу удалить лишние данные, такие как конверт и заголовок, но сохранить только данные о продукте, пожалуйста?

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<s:Header>
    <o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
        <u:Timestamp u:Id="_0">
            <u:Created>
                2019-10-17T13:53:13.751Z
            </u:Created>
            <u:Expires>
                2019-10-17T13:58:13.751Z
            </u:Expires>
        </u:Timestamp>
    </o:Security>
</s:Header>
<s:Body>
    <GetItemMastersResponse xmlns="http://www.xxxxx.com/xxxxx/2013/08">
        <GetItemMastersResult xmlns:a="http://schemas.datacontract.org/2004/07/xxxx.xxxx.xxx.Models.Inventory" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <a:ItemMaster>
                <a:Active>
                    false
                </a:Active>
                <a:Alias i:nil="true"/>
                <a:Attributes xmlns:b="http://schemas.datacontract.org/2004/07/xxxxxx.xxxx.xxxxxx.Models.Admin"/>
                <a:BarcodeFormat>
                    7
                </a:BarcodeFormat>
                <a:BarcodeNumber>
                    60000000425
                </a:BarcodeNumber>
                <a:CategoryId>
                    1
                </a:CategoryId>
                <a:Cost>
                    1250.0000
                </a:Cost>
                <a:DefaultPOLevel>
                    false
                </a:DefaultPOLevel>
                <a:DepartmentId>
                    1
                </a:DepartmentId>
                <a:Depth i:nil="true"/>
                <a:Description>
                    725 HUNTER 12G 28" M/C
                </a:Description>
            </a:ItemMaster>

1 Ответ

1 голос
/ 17 октября 2019

Вы можете использовать camaro для этой цели. напишите шаблон на основе xpath, чтобы указать, какие атрибуты вы хотели бы сохранить, как вы хотите назвать его в выводе json;что-то вроде ниже

enter image description here

пример

const { transform } = require('camaro')
const fs = require('fs')

const xml = fs.readFileSync('examples/ean.xml', 'utf-8')
const template = {
    cache_key: '/HotelListResponse/cacheKey',
    hotels: ['//HotelSummary', {
        hotel_id: 'hotelId',
        name: 'name',
        rooms: ['RoomRateDetailsList/RoomRateDetails', {
            rates: ['RateInfos/RateInfo', {
                currency: 'ChargeableRateInfo/@currencyCode',
                non_refundable: 'boolean(nonRefundable = "true")',
                price: 'number(ChargeableRateInfo/@total)'
            }],
            room_name: 'roomDescription',
            room_type_id: 'roomTypeCode'
        }]
    }],
    session_id: '/HotelListResponse/customerSessionId'
}

;(async function () {
    const result = await transform(xml, template)
    console.log(result)
})()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...