2012-03-08 69 views

Respuesta

9

node-xml2js biblioteca le ayudará.

var xml2js = require('xml2js'); 

var parser = new xml2js.Parser(); 
var xml = '\ 
<yyy:response xmlns:xxx="http://domain.com">\ 
    <yyy:success>\ 
     <yyy:data>some-value</yyy:data>\ 
    </yyy:success>\ 
</yyy:response>'; 
parser.parseString(xml, function (err, result) { 
    console.dir(result['yyy:success']['yyy:data']); 
}); 
-1

Hay un montón de métodos disponibles para extraer información de archivos XML, he utilizado XPath seleccione método para captar los valores de las variables. https://cloudtechnzly.blogspot.in/2017/07/how-to-extract-information-from-xml.html

+0

Vincular una solución no es una buena respuesta. Resuma la esencia de la solución aquí o proporcione una muestra de código. – TehSphinX

+0

revise este enlace, luego puede obtener algunas ideas https://cloudtechnzly.blogspot.in/2017/08/xml-to-json-conversion.html –

0

Usted no tiene que analizarlo para la extracción de datos, basta con utilizar expresiones regulares:

str = "<yyy:response xmlns:xxx='http://example.com'> <yyy:success><yyy:data>some-value</yyy:data> </yyy:success></yyy:response>"; 
    var re = new RegExp("<yyy:data>(.*?)</yyy:data?>", "gmi"); 

    while(res = re.exec(str)){ console.log(res[1])} 

// some-value 
2

Usted puede hacer que sea más fácil con camaro

const camaro = require('camaro') 

const xml = `<yyy:response xmlns:xxx='http://example.com'> <yyy:success><yyy:data>some-value</yyy:data> </yyy:success></yyy:response>` 

const template = { 
    data: '//yyy:data' 
} 

console.log(camaro(xml, template) 

Salida:

{ data: 'some-value' } 
Cuestiones relacionadas