3

我有以下 xml 文件作为程序的输出:

<?xml version='1.0' encoding='ISO-8859-1'?>
<rsp stat='ok'>
<results total='5'>
<result cover='http://ia.media-imdb.com/images/M/MV5BMjMyOTM4MDMxNV5BMl5BanBnXkFtZTcwNjIyNzExOA@@._V1._SX54_CR0,0,54,74_.jpg' title='The Amazing Spider-Man 'year='2012' director='Marc Webb' rating='7.3' details='/title/tt0948470/' />
<result cover='http://i.media-imdb.com/images/SF1f0a42ee1aa08d477a576fbbf7562eed/realm/feature.gif' title='The Amazing Spider-Man 2 'year='2014' director='Marc Webb' rating='N.A.' details='/title/tt1872181/'/>
<result cover='http://ia.media-imdb.com/images/M/MV5BMzk3MTE5MDU5NV5BMl5BanBnXkFtZTYwMjY3NTY3._V1._SX54_CR0,0,54,74_.jpg' title='Spider-Man 'year='2002' director='Sam Raimi' rating='7.3' details='/title/tt0145487/'/>
<result cover='http://ia.media-imdb.com/images/M/MV5BODUwMDc5Mzc5M15BMl5BanBnXkFtZTcwNDgzOTY0MQ@@._V1._SX54_CR0,0,54,74_.jpg' title='Spider-Man 3 'year='2007' director='Sam Raimi' rating='6.3' details='/title/tt0413300/'/>
<result cover='http://ia.media-imdb.com/images/M/MV5BMjE1ODcyODYxMl5BMl5BanBnXkFtZTcwNjA1NDE3MQ@@._V1._SX54_CR0,0,54,74_.jpg' title='Spider-Man 2 'year='2004' director='Sam Raimi' rating='7.5' details='/title/tt0316654/'/>
</results>
</rsp>

但是当我将它保存为 xml 文件并在浏览器中打开它时,它会显示属性构造错误。为什么 ??

4

2 回答 2

10

您必须使用空格字符将属性值与其他属性的名称分开。请注意,year紧跟在 的值之后title。你只是放错了几个空格。

title='The Amazing Spider-Man 'year='2012'

您必须将其更改为

title='The Amazing Spider-Man' year='2012'

以下文档应正确呈现。

<?xml version='1.0' encoding='ISO-8859-1'?>
<rsp stat='ok'>
  <results total='5'>
    <result cover='http://ia.media-imdb.com/images/M/MV5BMjMyOTM4MDMxNV5BMl5BanBnXkFtZTcwNjIyNzExOA@@._V1._SX54_CR0,0,54,74_.jpg' title='The Amazing Spider-Man' year='2012' director='Marc Webb' rating='7.3' details='/title/tt0948470/' />
    <result cover='http://i.media-imdb.com/images/SF1f0a42ee1aa08d477a576fbbf7562eed/realm/feature.gif' title='The Amazing Spider-Man 2' year='2014' director='Marc Webb' rating='N.A.' details='/title/tt1872181/'/>
    <result cover='http://ia.media-imdb.com/images/M/MV5BMzk3MTE5MDU5NV5BMl5BanBnXkFtZTYwMjY3NTY3._V1._SX54_CR0,0,54,74_.jpg' title='Spider-Man' year='2002' director='Sam Raimi' rating='7.3' details='/title/tt0145487/'/>
    <result cover='http://ia.media-imdb.com/images/M/MV5BODUwMDc5Mzc5M15BMl5BanBnXkFtZTcwNDgzOTY0MQ@@._V1._SX54_CR0,0,54,74_.jpg' title='Spider-Man 3' year='2007' director='Sam Raimi' rating='6.3' details='/title/tt0413300/'/>
    <result cover='http://ia.media-imdb.com/images/M/MV5BMjE1ODcyODYxMl5BMl5BanBnXkFtZTcwNjA1NDE3MQ@@._V1._SX54_CR0,0,54,74_.jpg' title='Spider-Man 2' year='2004' director='Sam Raimi' rating='7.5' details='/title/tt0316654/'/>
  </results>
</rsp>
于 2012-11-10T10:02:45.850 回答
3

每个属性前都需要一个空格。(你在“年”忘记了一个)

于 2012-11-10T10:00:59.843 回答