我有以下架构,我必须在其中找到列出的每个元素名称。
> <?xml version='1.0' encoding='UTF-8' ?> <xs:schema
> xmlns:xs='http://www.w3.org/2001/XMLSchema'>
>
>
> <xs:sequence> <xs:element name='name' type='xs:string'/> <xs:element
> name='address' type='xs:string'/> <xs:element name='city'
> type='xs:string'/> <xs:element name='country' type='xs:string'/>
> </xs:sequence>
>
> </xs:schema>
- 姓名
- 街道
- 城市
- 状态
- 国家
我尝试使用此代码:
var app = angular.module('httpApp', []);
app.controller('httpController', function ($scope, $http) {
var xmlm="<?xml version='1.0' encoding='UTF-8' ?><xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'><xs:sequence><xs:element name='name' type='xs:string'/><xs:element name='address' type='xs:string'/><xs:element name='city' type='xs:string'/><xs:element name='country' type='xs:string'/></xs:sequence></xs:schema>";
//var x2js = new X2JS();
$scope.data=xmlm;
//var aftCnv = x2js.xml_str2json(xmlm);
//$scope.jsonic=aftCnv;
xmlDoc = $.parseXML( xmlm ),
$xml = $( xmlDoc ),
$name = $xml.find( "xs:sequence" );
$scope.data=$name.text();
$( "#1" ).append( $name.text() );
我在控制台或输出上都没有收到错误。该页面为空。
最佳答案
您可以使用这个jQuery XML to JSON Plugin .
示例(带有您的 xml 字符串):
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.3.min.js"></script>
<script type="text/javascript" src="https://jquery-xml2json-plugin.googlecode.com/svn/trunk/jquery.xml2json.js"></script>
<script type="text/javascript">
$(function() {
var
xmlm,
json,
elements;
xmlm =
"<?xml version='1.0' encoding='UTF-8' ?>" +
"<xs:schema" +
" xmlns:xs='http://www.w3.org/2001/XMLSchema'>" +
" <xs:sequence>" +
" <xs:element name='name' type='xs:string'/>" +
" <xs:element name='address' type='xs:string'/>" +
" <xs:element name='city' type='xs:string'/>" +
" <xs:element name='country' type='xs:string'/>" +
" </xs:sequence>" +
"</xs:schema>";
json = $.xml2json(xmlm);
elements = json.sequence.element;
for (var i in elements) {
console.log(elements[i].name);
}
});
</script>
结果:
姓名
地址
城市
国家
关于javascript - 在 JavaScript 中使用 XML 解析器解析 XML 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36787400/