Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用xml2js parseString返回值_Node.js_Json_Xml_Xml2js - Fatal编程技术网

Node.js 使用xml2js parseString返回值

Node.js 使用xml2js parseString返回值,node.js,json,xml,xml2js,Node.js,Json,Xml,Xml2js,您可以使用async/await模式来执行此操作,例如: parseString(xml, function (err, result) { return result }); const parseString=require('xml2js')。parseString; const xmlInput='318881DFS标准房间0.00002123456 Jon Snow102019-03-07T13:43:0022019-03-08T12:00:00 FalseF

您可以使用async/await模式来执行此操作,例如:

parseString(xml, function (err, result) {
        return result
    });
const parseString=require('xml2js')。parseString;
const xmlInput='318881DFS标准房间0.00002123456 Jon Snow102019-03-07T13:43:0022019-03-08T12:00:00 FalseFalseCheckInCleannebleSaleroomFalseFalse1FalseNormal0None0';
函数解析xml(xml){
返回新承诺((解决、拒绝)=>{
解析字符串(xml,(错误,结果)=>{
如果(错误){
拒绝(错误);
}否则{
决心(结果);
}
});
});
}
函数processResult(结果){
log(“processResult:result:,result”);
}
异步函数testXmlParse(xml){
试一试{
让结果=等待解析xml(xml);
//现在你有了结果,你可以做进一步的处理,写入文件等。
处理结果(result);
}捕捉(错误){
错误(“解析XML失败:”,错误);
}
}
testXmlParse(xmlInput);

您可以使用short方法

const parseString = require('xml2js').parseString;
const xmlInput = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetCurrentRoomStatusGraphResponse xmlns="www.example.com"><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);
const parseString = require('xml2js').parseString;
const xmlInput = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetCurrentRoomStatusGraphResponse xmlns="www.example.com"><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);

import { parseStringPromise } from 'xml2js';


xmlresponse = `<soap:Envelope
        xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <soap:Header>
            <QAInformation
                xmlns="http://www.qas.com/OnDemand-2011-03">
                <StateTransition>SearchResults</StateTransition>
                <CreditsUsed>1</CreditsUsed>
            </QAInformation>
        </soap:Header>
        <soap:Body>
            <Address
                xmlns="http://www.qas.com/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']);

[
  {
    '$': { 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' ]
  }
]