将新元素添加到我的xml文件使其无法显示

时间:2015-10-31 10:28:03

标签: javascript html xml

所以练习XML,因为我暂时没有这样做。当我在我的.xml文件中使用它时,当我使用html / js在页面上显示它时,它显示正常。

<?xml version="1.0" encoding="UTF-8"?>
<text xmlns:xlink="http://www.w3.org/1999/xlink">
    <book id="1">
        <title>Virgil's Aeneid</title>
        <paragraph id="1">
            <line>  Arms and the man I sing, who first from the &lt;a href="#" id="coastsOf" onclick="getInfo('coastsOf')"&gt; coasts of &lt;/a&gt; Troy,  </line>
            <line>  exiled by fate, came to Italy and Lavine shores; much buf-  </line>
            <line>  fetted on sea and land by  violence from above, through  </line>
            <line>  cruel Juno's unforgiving wrath, and much enduring in war  </line>
            <line>  also, till he should build a city and bring his gods to Latium;  </line>
            <line>  Whence came the Latin race, the lords of Alba, and the  </line>
            <line>  lofty walls of Rome.  </line>
        </paragraph>

        <paragraph id="2">
            <line>  Tell me, O Muse, the cause; wherein thwarted in will  </line>  
            <line>  or wherefore angered, did the Queen of heaven drive a  </line>
            <line>  man, of goodness so wondrous, to traverse so many perils,  </line>
            <line>  to face so many toils. Can heavenly spirits cherish resent-  </line>
            <line>  ment so dire?  </line>
        </paragraph>
    </book>
</text>

但是,当我添加一个新块时:

 <paragraph id="3">
        <line>  There was an ancient city, the home of Tyrian settlers,  </line> 
        <line>  Carthage, over against Italy and the Tiber's mouths afar,  </line>  
        <line>  rich in wealth and stern in war's pursuits. This, ‘tis said,  </line> 
        <line>  Juno loved above all other lands, holding Samos itself less  </line> 
        <line>  dear. Here was her armour, here her chariot; that here  </line> 
        <line>  should be the capital of the nations, should the fates pre-  </line>
        <line>  chance allow it, was even then the goddess's aim and cher-  </line>
        <line>  ished hope. Yet in truth she had heard that a race was  </line>
        <line>  springing from Trojan blood, to overthrow some day the  </line>
        <line>  Tyrian towers; that from it a people, kings of broad realms  </line>
        <line>  and proud in war, should come forth for Libya's downfall:  </line>
        <line>  so rolled the wheel of fate. The daughter of Saturn, fearful  </line>
        <line>  of this and mindful of the old war which erstwhile she had  </line>
        <line>  fought at Troy for her beloved Argos – not yet, too, had the  </line>
        <line>  cause of her wrath and her bitter sorrows faded from her  </line>
        <line>  mind: deep in her heart remain the judgement of Paris and  </line>
        <line>  the outrage to her slighted beauty, her hatred of the race  </line>
        <line>  and the honours paid to the ravished Ganymede – inflamed  </line>
        <line>  hereby yet more, she tossed on the wide main the Trojan  </line>
        <line>  remnant, left by the Greeks and pitiless Achilles, and kept  </line>
        <line>  them far from Latium; and many a year they wandered,  </line>
        <line>  driven by the fates o'er all the seas. So vast was the effort to  </line>
        <line>  found the Roman race.  </line>
    </paragraph>

它不再显示在网页上。

我是否遗漏了应该如何构建我的xml文件的内容?

编辑:我用来呈现它的HTML是

    <html>
     <head>     </head>
     <body>

     <div id="showLines"></div>

     <div id="showRefs"></div>

     <script>
     displayText(0);

     function displayText(i) {
         var xmlhttp = new XMLHttpRequest();
         xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                getText(xmlhttp, i)
        }
       }
    xmlhttp.open("GET", "Aeneid.xml", true);
        xmlhttp.send();
     }

     function getText(xml, i) {
         var xmlDoc = xml.responseXML;
         x = xmlDoc.getElementsByTagName("line");
         for(j = 0; j < x.length; j++){
        document.getElementById("showLines").innerHTML =  document.getElementById("showLines").innerHTML + 
            x[j].childNodes[0].nodeValue + "<br>";
   }
   }



function getInfo(ref) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            getRef(xmlhttp, ref)
        }
    }
    xmlhttp.open("GET", "references.xml", true);
    xmlhttp.send();
}

function getRef(xml, ref) {
    var xmlDoc = xml.responseXML;
    x = xmlDoc.getElementsByTagName("link");

    for(j = 0; j < x.length; j++)   {
        if(x[j].id == ref) {
            document.getElementById("showRefs").innerHTML = document.getElementById("showRefs").innerHTML + 
                "Title: " + x[j].childNodes[1].firstChild.nodeValue + "<br>" +
                "Text: " + x[j].childNodes[3].firstChild.nodeValue + "<br>";
            if (x[j].childNodes[5].firstChild.nodeValue) {
                document.getElementById("showRefs").innerHTML = document.getElementById("showRefs").innerHTML + "Image One: " + x[j].childNodes[5].firstChild.nodeValue + "<br>";
                if (x[j].childNodes[7].firstChild.nodeValue) {
                    document.getElementById("showRefs").innerHTML = document.getElementById("showRefs").innerHTML + "Image Two: " + x[j].childNodes[7].firstChild.nodeValue + "<br>";
                }
            }
        }
    }

}

</script>

</body>
</html>

0 个答案:

没有答案