Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

111
Visualizações
extract token from soap xml response

I'm trying to extract a token from a SOAP xml response. I've tried various snippets and have come up with the following:

var xml2js = require('xml2js');
var parser = new xml2js.Parser();


var options = {
  "method": "POST",
  "hostname": "www.titlepage.com",
  "port": null,
  "path": "/ws/TitleQuery.php",
  "headers": {
    "content-type": "text/xml",
    "cache-control": "no-cache",
    
  }
};

  


var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    var body = Buffer.concat(chunks);
    const xml = (body.toString());
    
    

    parser.parseString(xml, function (err, result) {
      if (err) {
          console.error('here is the eror: ', err);
      } else {
          jsonLog = (JSON.stringify(result, null, 2));
          console.log(result['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['ns1:LoginResponse'][0]['Token']['0']); 
      }
  });

  });
});

req.write("<?xml version=\"1.0\"?>\n<soap:Envelope xmlns:soap=\"http://www.w3.org/2001/12/soap-envelope\" soap:encodingStyle=\"http://www.w3.org/2001/12/soap-encoding\">\n  <soap:Body xmlns:m=\"http://www.titlepage.com/ws\">\n    <m:Login>\n      <m:UserName>a105yn</m:UserName>\n      <m:Password>pink6PUPT1yult*kul</m:Password>\n    </m:Login>\n  </soap:Body>\n</soap:Envelope>");
req.end();

The outcome is:

  {
    _: '312cb0ee6c346ee266dc95bc6c688021',
    '$': { 'xsi:type': 'xsd:string' }
  }
]

How do I go the final step and access the value of the token (_ key) and, what is the _ key?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can try this.

var convert = require('xml-js');

var xml = `<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
    <soap:Body xmlns:m="http://www.titlepage.com/ws">
        <m:Login>
            <m:UserName>Username goes here</m:UserName>
            <m:Password>Password goes here</m:Password>
        </m:Login>
    </soap:Body>
</soap:Envelope>`

var options = {compact: true, spaces: 4, elementNameFn: function(val) {return val.replace('soap:','');}};
var result = convert.xml2json(xml,options);


console.log(result);


var extract = JSON.parse(result)
console.log(extract.Envelope.Body['m:Login']['m:UserName']["_text"]) //Prints "Username goes here"
console.log(extract.Envelope.Body['m:Login']['m:Password']["_text"]) //Prints "Password goes here"
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda