문제

I have the following code :

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>

<p id="someElement"></p>
<p id="anotherElement"></p>



<script>
var xml = "<count>1</count><ticketID id='2'><incidentUrl>3</incidentUrl></ticketID>",
xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc ),
$title = $xml.find( "count" );

/* append "RSS Title" to #someElement */
$( "#someElement" ).append( $title.text() );

/* change the title to "XML Title" */
$title.text( "XML Title" );

/* append "XML Title" to #anotherElement */
$( "#anotherElement" ).append( $title.text() );
</script>

</body>
</html>

The above code - produces an error : Invalid XML

However , when i change var xml to : var xml = < count >1< / count > it works without any errors.

Why is this ?

Thanks!

도움이 되었습니까?

해결책

You cannot have two root elements in an XML document.

Note that according to the official syntax there can be only one "top-level" or "root" element.

Your example has both count and ticketId at the top-level, which is not allowed.

다른 팁

Your XML has multiple root element

<count>1</count><ticketID id='2'><incidentUrl>3</incidentUrl></ticketID>

Wrap this inside a single container root element and it should work

<container><count>1</count><ticketID id='2'><incidentUrl>3</incidentUrl></ticketID></container>

jQuery.parseXML() Parses a string into an XML document. Your question..

However, when i change var xml to : var xml = < count >1< / count > it works without any errors.

Why is this?

Thats because your data is already in XML format and doesn't need parsing. Just continue with the next line of code.

$xml = $( xmlDoc ),
$title = $xml.find( "count" );

No need to parseXML.
Hope it helps

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top