内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list($forumlist, $model = 0, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $model . '-' . $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 不分模型 * @param int $display 0全部CMS栏目 1在首页和频道显示内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list_show($forumlist, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 * @return mixed BBS栏目数据(仅列表) 尚未开放bbs频道功能 */ function forum_list($forumlist) { if (empty($forumlist)) return array(); static $cache = array(); if (isset($cache['bbs_forum_list'])) return $cache['bbs_forum_list']; $cache['bbs_forum_list'] = array(); foreach ($forumlist as $_fid => $_forum) { if ($_forum['type']) continue; $cache['bbs_forum_list'][$_fid] = $_forum; } return $cache['bbs_forum_list']; } // 导航显示的版块 function nav_list($forumlist) { if (empty($forumlist)) return NULL; static $cache = array(); if (isset($cache['nav_list'])) return $cache['nav_list']; foreach ($forumlist as $fid => $forum) { if (0 == $forum['nav_display']) { unset($forumlist[$fid]); } } return $cache['nav_list'] = $forumlist; } ?>javascript - use xml2js parseString return value - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - use xml2js parseString return value - Stack Overflow

programmeradmin1浏览0评论
xml = `
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="/" xmlns:xsd="; xmlns:xsi=";>
   <soap:Body>
      <GetCurrentRoomStatusGraphResponse xmlns="www.example">

         ...

         </GetCurrentRoomStatusGraphResult>
      </GetCurrentRoomStatusGraphResponse>
   </soap:Body>
</soap:Envelope>
`


var parseString = require('xml2js').parseString;
parseString(xml, function (err, result) {
    console.dir(result);
});

I want an another function to use this value, how to return the value like this?

parseString(xml, function (err, result) {
  return result
});
xml = `
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap/soap/envelope/" xmlns:xsd="http://www.w3/2001/XMLSchema" xmlns:xsi="http://www.w3/2001/XMLSchema-instance">
   <soap:Body>
      <GetCurrentRoomStatusGraphResponse xmlns="www.example.">

         ...

         </GetCurrentRoomStatusGraphResult>
      </GetCurrentRoomStatusGraphResponse>
   </soap:Body>
</soap:Envelope>
`


var parseString = require('xml2js').parseString;
parseString(xml, function (err, result) {
    console.dir(result);
});

I want an another function to use this value, how to return the value like this?

parseString(xml, function (err, result) {
  return result
});
Share Improve this question edited Aug 16, 2022 at 14:11 snoob dogg 2,8854 gold badges36 silver badges65 bronze badges asked Mar 7, 2019 at 6:26 毕兴涛毕兴涛 631 gold badge1 silver badge4 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 3

You can use the async / await pattern to do this, for example:

const parseString = require('xml2js').parseString;
const xmlInput = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap/soap/envelope/" xmlns:xsi="http://www.w3/2001/XMLSchema-instance" xmlns:xsd="http://www.w3/2001/XMLSchema"><soap:Body><GetCurrentRoomStatusGraphResponse xmlns="www.example."><GetCurrentRoomStatusGraphResult><RoomStatusGraphItem><RoomID>3</RoomID><ChainID>1</ChainID><RoomNo>8888</RoomNo><RoomTypeID>1</RoomTypeID><RoomTypeCode>DF</RoomTypeCode><RoomTypeName>standard room</RoomTypeName><RoomRate>0.0000</RoomRate><Floor>2</Floor><FolioID>123456</FolioID><Guest><RoomStatusGuest><Name>Jon Snow</Name><Sex>1</Sex><VipTypeID>0</VipTypeID></RoomStatusGuest></Guest><Arrival>2019-03-07T13:43:00</Arrival><Depart>2019-03-08T12:00:00</Depart><IsDepart>false</IsDepart><IsTimeRoom>false</IsTimeRoom><Surreptitious>false</Surreptitious><CheckInState>CheckIn</CheckInState><ClentState>NoClean</ClentState><HouseKeepState>EnableSaleRoom</HouseKeepState><CheckRoomFlag>false</CheckRoomFlag><IsBookInRoom>false</IsBookInRoom><IsFreeRoom>false</IsFreeRoom><IsInnerRoom>true</IsInnerRoom><IsThirdOTA>false</IsThirdOTA><IsAssociationRoom>false</IsAssociationRoom><RoomRemark /><FolioInnerRemark /><FolioRemark /><AddtionalFlag><int>1</int></AddtionalFlag><IsReserve>false</IsReserve><ClearRoomType>3</ClearRoomType><ClearRoomTypeName>normal</ClearRoomTypeName><CheckOutID>0</CheckOutID><CheckOutState>None</CheckOutState><MilkMarketStatus>0</MilkMarketStatus></RoomStatusGraphItem></GetCurrentRoomStatusGraphResult></GetCurrentRoomStatusGraphResponse></soap:Body></soap:Envelope>';

function parseXml(xml) {
    return new Promise((resolve, reject) => {
        parseString(xml, (err, result) => {
            if (err) {
                reject(err);
            } else {
                resolve(result);
            }
        });
    });
}

function processResult(result) {
    console.log("processResult: result: ", result);
}

async function testXmlParse(xml) {
    try {
        let result = await parseXml(xml);
        // Now that you have the result you can do further processing, write to file etc.
        processResult(result);
    } catch (err) {
        console.error("parseXml failed: ", err);
    }
}

testXmlParse(xmlInput);

you can use the short method


import { parseStringPromise } from 'xml2js';


xmlresponse = `<soap:Envelope
        xmlns:soap="http://schemas.xmlsoap/soap/envelope/"
        xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
        xmlns:xsd="http://www.w3/2001/XMLSchema">
        <soap:Header>
            <QAInformation
                xmlns="http://www.qas./OnDemand-2011-03">
                <StateTransition>SearchResults</StateTransition>
                <CreditsUsed>1</CreditsUsed>
            </QAInformation>
        </soap:Header>
        <soap:Body>
            <Address
                xmlns="http://www.qas./OnDemand-2011-03">
                <QAAddress DPVStatus="DPVNotConfigured">
                    <AddressLine LineContent="None">
                        <Label />
                        <Line>Commonwealth Bank Building</Line>
                    </AddressLine>
                    <AddressLine LineContent="None">
                        <Label />
                        <Line>L 10 71-89 Adelaide St</Line>
                    </AddressLine>
                    <AddressLine LineContent="None">
                        <Label />
                        <Line />
                    </AddressLine>
                    <AddressLine>
                        <Label>Locality</Label>
                        <Line>BRISBANE CITY</Line>
                    </AddressLine>
                    <AddressLine>
                        <Label>State code</Label>
                        <Line>QLD</Line>
                    </AddressLine>
                    <AddressLine>
                        <Label>Postcode</Label>
                        <Line>4000</Line>
                    </AddressLine>
                    <AddressLine>
                        <Label>Country</Label>
                        <Line>AUSTRALIA</Line>
                    </AddressLine>
                    <AddressLine LineContent="Ancillary">
                        <Label>DPID/DID</Label>
                        <Line>87902264</Line>
                    </AddressLine>
                </QAAddress>
            </Address>
        </soap:Body>
    </soap:Envelope>`

        let convert = await parseStringPromise(xmlresponse);
        convert = (JSON.parse(JSON.stringify(convert)));

        console.log(convert['soap:Envelope']['soap:Body'][0]['Address'][0]['QAAddress'][0]['AddressLine']);

you will have something like this

[
  {
    '$': { LineContent: 'None' },
    Label: [ '' ],
    Line: [ 'Commonwealth Bank Building' ]
  },
  {
    '$': { LineContent: 'None' },
    Label: [ '' ],
    Line: [ 'L 10 71-89 Adelaide St' ]
  },
  { '$': { LineContent: 'None' }, Label: [ '' ], Line: [ '' ] },
  { Label: [ 'Locality' ], Line: [ 'BRISBANE CITY' ] },
  { Label: [ 'State code' ], Line: [ 'QLD' ] },
  { Label: [ 'Postcode' ], Line: [ '4000' ] },
  { Label: [ 'Country' ], Line: [ 'AUSTRALIA' ] },
  {
    '$': { LineContent: 'Ancillary' },
    Label: [ 'DPID/DID' ],
    Line: [ '87902264' ]
  }
]
发布评论

评论列表(0)

  1. 暂无评论