2012-06-05 4 views
5

xml2js를 사용하여 xml 내의 요소를 구문 분석하고 쿼리하려고합니다. 내가 원하는 것은 xml2js를 사용하여 xml을 구문 분석 할 노드

var xml = "<config><test>Hello</test><data>SomeData</data></config>"; 

이 값을 추출하고 여기에 var extractedData

에 할당 지금까지이 작업은 다음과 같습니다 :

var parser = new xml2js.Parser(); 
parser.parseString(xml, function(err,result){ 
    //Extract the value from the data element 
    extractedData = result['data']; 
} 

이 작동하지 않는 다음과 같이 내 XML 문자열입니다 . 누군가 내 XML에서 값을 가져올 수있는 방법을 지적 할 수 있습니까?

감사합니다.

이것은 작동하지 않는 것 같습니다. 누군가 여기서 쟁점이 될 수 있다고 말해 줄 수 있습니까?

답변

14

은 나를

var xml2js = require('xml2js'); 
var xml = "<config><test>Hello</test><data>SomeData</data></config>"; 

var extractedData = ""; 
var parser = new xml2js.Parser(); 
parser.parseString(xml, function(err,result){ 
    //Extract the value from the data element 
    extractedData = result['config']['data']; 
    console.log(extractedData); 
}); 
console.log("Note that you can't use value here if parseString is async; extractedData=", extractedData); 

결과를 작동합니다

SomeData 
Note that you can't use value here if parseString is async; extractedData= SomeData 
+3

가 어떻게 깊이 –

+0

의 5 ~ 6 수준에 데이터 @Vishwanathgowdak 사용을 인쇄 할 수 있습니다'util.inspect()'; 이에 대한 간단한 예를 보려면 대답을 참조하십시오. http://stackoverflow.com/questions/10729276/how-can-i-get-the-full-object-in-node-js-console-log-rather-than- object 다음은 빠르고 더러운 접근법이다 :'console.log (require ('util'). inspect (result, false, null));' – Josh1billion

관련 문제