引用 XML 文件的 html DOM 文件不会在浏览器中打印

发布于 2024-09-30 23:53:32 字数 3864 浏览 4 评论 0原文

我对此完全陌生,我正在上一堂有趣的课,但现在却变成了一个大麻烦。我有一个 html DOM 文件,它引用了一个 XML 文件,我需要将其显示在浏览器的屏幕上。它应该只打印标题、第一个、最后一个、位置、描述等元素。 CSS 和 Schema 都经过验证并且可以正常工作。当我使用 html 文件时,我还应该将它们都保留在 xml 文件中吗?我的浏览器屏幕上出现一片空白。 谢谢,利亚姆

<html>

<head>

    <title>Catalog</title>

    <script type="text/javascript">

         var xmlDoc;

         function loadXMLDoc() {

              // XML loader for IE

              if (window.ActiveXObject) {

                  xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

                  xmlDoc.load("catalog.xml");

                 printCatalog();

            }

            // XML loader for other browsers

           else {

                xmlDoc = document.implementation.createDocument("", "", null);

                xmlDoc.load("catalog.xml");

                xmlDoc.onload = printCatalog;

          }
      }


      function printcatalog() {

          var titleNodes = xmlDoc.getElementsByTagName("title");

          var firstNodes = xmlDoc.getElementsByTagName("first");

          var lastNodes = xmlDoc.getElementsByTagName("last");

          var locationNodes = xmlDoc.getElementsByTagName("location");

         var descriptionNodes = xmlDoc.getElementsByTagName("description");

            for (var i = 0; i < titleNodes.length; i++) {

              document.write("<div style='font-family:arial; font-weight:bold;

                                      color:red'>" + 

                                    titleNodes[i].firstChild.nodeValue + "</div>");

              document.write("<div style='font-family:arial'>" +

                                     firstNodes[i].firstChild.nodeValue + "<br />");

              document.write(lastNodes[i].firstChild.nodeValue + "<br />");

             document.write(firstNodes[i].firstChild.nodeValue + "<br />");

             document.write(locationNodes[i].firstChild.nodeValue + "<br />");

             document.write(descriptionNodes[i].firstChild.nodeValue + "</div>");
           }


       }

 </script>

 </head>

<body onload="loadXMLDoc()">

</body>

</html>







 <?xml version="1.0"?>

 <?xml-stylesheet href="catalog.css" type="text/css"?>

 <catalog xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"

 xs:noNamespaceSchemaLocation="catalog.xsd"> 

 <course courseNum="CS205" area="ComputerScience">

 <title>Programming 1</title>

 <instructor>

 <first>Bill</first>

 <last>Gates</last>

 </instructor>

 <units>3</units>

<location>

<building>100</building>

<room>101</room>

</location>

 <description>Learn from the man himself, Mr. Microsoft. 

             Must love PCs and not Macs.</description>

</course>

<course courseNum="SS305" area="SocialStudies">

<title>Government 1945</title>

<instructor>

 <first>Karl</first>

 <last>Marx</last>

 </instructor>

<units>3</units>

<location>

<building>200</building>

<room>202</room>

</location>

<description>Mr. Marx will explain his political theories

          Lecture course. Bring lots of coffee.</description>
 </course>

<course courseNum="MA350" area="Math">

<title>Math 911</title>

 <instructor>

 <first>Albert</first>

 <last>Einstein</last>

 </instructor>

 <units>3</units>

<location>

<building>200</building>

 <room>302</room>

</location>

 <description>Find the answers to the universe in just one semester.

  </description>

</course>

  </catalog>

I am totally new to this and I am taking a class for fun that has now turned into a big headache. I have this html DOM file that references an XML file that I need to have show up on the screen in a browser. It should only print the elements of, title, first, last, location, discription. The CSS and Schema both validate fine and work. Should I still leave both of them in the the xml file when I using the html file? I get a blank on the browser screen.
Thanks, Liam

<html>

<head>

    <title>Catalog</title>

    <script type="text/javascript">

         var xmlDoc;

         function loadXMLDoc() {

              // XML loader for IE

              if (window.ActiveXObject) {

                  xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

                  xmlDoc.load("catalog.xml");

                 printCatalog();

            }

            // XML loader for other browsers

           else {

                xmlDoc = document.implementation.createDocument("", "", null);

                xmlDoc.load("catalog.xml");

                xmlDoc.onload = printCatalog;

          }
      }


      function printcatalog() {

          var titleNodes = xmlDoc.getElementsByTagName("title");

          var firstNodes = xmlDoc.getElementsByTagName("first");

          var lastNodes = xmlDoc.getElementsByTagName("last");

          var locationNodes = xmlDoc.getElementsByTagName("location");

         var descriptionNodes = xmlDoc.getElementsByTagName("description");

            for (var i = 0; i < titleNodes.length; i++) {

              document.write("<div style='font-family:arial; font-weight:bold;

                                      color:red'>" + 

                                    titleNodes[i].firstChild.nodeValue + "</div>");

              document.write("<div style='font-family:arial'>" +

                                     firstNodes[i].firstChild.nodeValue + "<br />");

              document.write(lastNodes[i].firstChild.nodeValue + "<br />");

             document.write(firstNodes[i].firstChild.nodeValue + "<br />");

             document.write(locationNodes[i].firstChild.nodeValue + "<br />");

             document.write(descriptionNodes[i].firstChild.nodeValue + "</div>");
           }


       }

 </script>

 </head>

<body onload="loadXMLDoc()">

</body>

</html>







 <?xml version="1.0"?>

 <?xml-stylesheet href="catalog.css" type="text/css"?>

 <catalog xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"

 xs:noNamespaceSchemaLocation="catalog.xsd"> 

 <course courseNum="CS205" area="ComputerScience">

 <title>Programming 1</title>

 <instructor>

 <first>Bill</first>

 <last>Gates</last>

 </instructor>

 <units>3</units>

<location>

<building>100</building>

<room>101</room>

</location>

 <description>Learn from the man himself, Mr. Microsoft. 

             Must love PCs and not Macs.</description>

</course>

<course courseNum="SS305" area="SocialStudies">

<title>Government 1945</title>

<instructor>

 <first>Karl</first>

 <last>Marx</last>

 </instructor>

<units>3</units>

<location>

<building>200</building>

<room>202</room>

</location>

<description>Mr. Marx will explain his political theories

          Lecture course. Bring lots of coffee.</description>
 </course>

<course courseNum="MA350" area="Math">

<title>Math 911</title>

 <instructor>

 <first>Albert</first>

 <last>Einstein</last>

 </instructor>

 <units>3</units>

<location>

<building>200</building>

 <room>302</room>

</location>

 <description>Find the answers to the universe in just one semester.

  </description>

</course>

  </catalog>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南风起 2024-10-07 23:53:32

页面加载后不能使用document.write,因为它会覆盖已经加载的 DOM。

你应该这样做:

<body>
   <script> 
       loadXMLDoc(); // call it while the page is loading
   </script>
</body>

或者如果你不想调用 loadXMLDoc();早期,您可以将 document.write 更改为其他内容。可能是一个丑陋的版本:

document.body.innerHTML += 'somestuff'

或者将文本节点附加到正文:

var doc = document.createTextNode('text here');
document.body.appendChild(doc);

You cannot use document.write after the page is loaded, because it will overwrite the already loaded DOM.

You should do something like:

<body>
   <script> 
       loadXMLDoc(); // call it while the page is loading
   </script>
</body>

Or if you don't want to call the loadXMLDoc(); early, you can change document.write to something else. May be an ugly version would be:

document.body.innerHTML += 'somestuff'

Or append text nodes to the body:

var doc = document.createTextNode('text here');
document.body.appendChild(doc);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文