将 XML 文件导入到 HTML 页面中,然后我想在导入的 XML 标签上使用 DOM

发布于 2024-12-14 06:08:07 字数 2841 浏览 0 评论 0原文

我有一点(我认为:))问题。 我有一个名为 index.html 的页面 HTML,如下所示:

<html>
<head>
<script src="script.js" type="text/javascript" ></script>
</head>
<body>

<div id="MyButtons">
 Here there are some buttons that call some javascripts functinons of file script.js
</body>

</html>

然后我还有一个简单的 XML 文件,如下所示:

<?xml version="1.0">
 <testo>
  <paragrafo id="p_01">
    <soggetto>Steve</soggetto> <verbo>is</verbo> a <complemento>man</complemento>.
    <soggetto>Mark</soggetto> <verbo>is</verbo> another <complemento>man</complemento>.
  </paragrafo>

  <paragrafo id="p_02">
    <soggetto>Tony</soggetto> <verbo>is</verbo> a <complemento>cousin of Steve</complemento>.
    <soggetto>Maria</soggetto> <verbo>is</verbo> another <complemento>cousinf of Steve</complemento>.
  </paragrafo>
  .
  .
  .other elements <paragrafo>
  .
  .
</testo>

我创建了我的个人标签,如 ecc。

我希望 onClick 在 div="MyButton" 中的我的按钮上启动一个假设的函数 javascript loadXML('file.xml',"divId") 复制所有内容file.xml(文本和标签)并将其放在 index 主体中的 div id="divId"(由函数 loadXML 创建)上.html。 在 loadXML 函数之后,我希望我的 html 页面如下所示:

<html>
<head>
 <script src="script.js" type="text/javascript" ></script>
</head>
<body>
 <div id="divId">

  <testo>
    <paragrafo id="p_01">
     <soggetto>Steve</soggetto> <verbo>is</verbo> a <complemento>man</complemento>.
     <soggetto>Mark</soggetto> <verbo>is</verbo> another <complemento>man</complemento>.
    </paragrafo>

    <paragrafo id="p_02">
     <soggetto>Tony</soggetto> <verbo>is</verbo> a <complemento>cousin of Steve</complemento>.
     <soggetto>Maria</soggetto> <verbo>is</verbo> another <complemento>cousinf of Steve</complemento>.
    </paragrafo>
    .
    .
    .other elements <paragrafo>
    .
    .
 </testo>

 </div>

 <div id="MyButtons">
  Here there are some buttons that call some javascripts functinons of file script.js
 </div>
 </body>

</html>

以这种方式,在我想使用 index.html 的按钮来调用使用 document.getElementById('p_01') 的函数之后;以及DOM的其他功能,就好像file.xml的内容已经在默认页面index.html中一样。

我如何编写函数 loadXML("file.xml","divID") 来执行此操作? 我希望你能帮助我,请几天我尝试这样做但没有任何结果:(

非常感谢!

I have a little (i think :) ) problem.
I have a page HTML called index.html like that:

<html>
<head>
<script src="script.js" type="text/javascript" ></script>
</head>
<body>

<div id="MyButtons">
 Here there are some buttons that call some javascripts functinons of file script.js
</body>

</html>

Then i have also a simple XML file like this:

<?xml version="1.0">
 <testo>
  <paragrafo id="p_01">
    <soggetto>Steve</soggetto> <verbo>is</verbo> a <complemento>man</complemento>.
    <soggetto>Mark</soggetto> <verbo>is</verbo> another <complemento>man</complemento>.
  </paragrafo>

  <paragrafo id="p_02">
    <soggetto>Tony</soggetto> <verbo>is</verbo> a <complemento>cousin of Steve</complemento>.
    <soggetto>Maria</soggetto> <verbo>is</verbo> another <complemento>cousinf of Steve</complemento>.
  </paragrafo>
  .
  .
  .other elements <paragrafo>
  .
  .
</testo>

I have created my personal tag like <testo>, <soggetto>, <verbo> ecc.

I want that onClick on a my button in div="MyButton" launch an hypothetical function javascript loadXML('file.xml',"divId") that copy all the content of file.xml (text and tag) and put it on a div whit id="divId"(that the function loadXML creates) in the body of index.html.
After the loadXML function i want that my html page look like this:

<html>
<head>
 <script src="script.js" type="text/javascript" ></script>
</head>
<body>
 <div id="divId">

  <testo>
    <paragrafo id="p_01">
     <soggetto>Steve</soggetto> <verbo>is</verbo> a <complemento>man</complemento>.
     <soggetto>Mark</soggetto> <verbo>is</verbo> another <complemento>man</complemento>.
    </paragrafo>

    <paragrafo id="p_02">
     <soggetto>Tony</soggetto> <verbo>is</verbo> a <complemento>cousin of Steve</complemento>.
     <soggetto>Maria</soggetto> <verbo>is</verbo> another <complemento>cousinf of Steve</complemento>.
    </paragrafo>
    .
    .
    .other elements <paragrafo>
    .
    .
 </testo>

 </div>

 <div id="MyButtons">
  Here there are some buttons that call some javascripts functinons of file script.js
 </div>
 </body>

</html>

In this way after i want use the buttons of index.html for call functions that use document.getElementById('p_01'); and other function of DOM, as if the content of file.xml was already in the default page index.html.

How i can write the funciont loadXML("file.xml","divID") to do this?
I hope you can help me, please are days that i try to do this without any result :(

Thank you at all !!!

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

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

发布评论

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

评论(3

野生奥特曼 2024-12-21 06:08:07

查看 Microsoft.XMLDOM FOR IE 和
document.implementation.createDocument()

要加载 xml 文档,您可以执行以下操作。

  if (window.ActiveXObject){
    var xmldoc=new ActiveXObject("Microsoft.XMLDOM");
     }else if (document.implementation && document.implementation.createDocument){
       xmldoc=document. implementation.createDocument("", "doc", null);
    }
   xmldoc.async = false;
   xmldoc.load("file.xml");

你需要做一些工作来获取节点

Have a look in to Microsoft.XMLDOM FOR IE and
document.implementation.createDocument()

To load xml document you could do the following.

  if (window.ActiveXObject){
    var xmldoc=new ActiveXObject("Microsoft.XMLDOM");
     }else if (document.implementation && document.implementation.createDocument){
       xmldoc=document. implementation.createDocument("", "doc", null);
    }
   xmldoc.async = false;
   xmldoc.load("file.xml");

you will need to do bit of work to get the nodes

嗫嚅 2024-12-21 06:08:07

我认为这是不可能的。问题是 getElementById 仅适用于 HTML DOM 对象,并且“paragrafo”不是有效的 DOM 对象(“p”将是一个),我的意思是,仅仅拥有格式良好的 html 标签是不够的,它们还必须是 html 有效标签。

您应该告诉我们用户按下按钮后应用程序中会发生什么?该按钮有什么作用?

I don't think that's possible. The problem is that getElementById works only on HTML DOM objects, and "paragrafo" is not a valid DOM Object ("p" would be one), I mean, it's not enough to have well-formed html tags, they must also be html valid tags.

You should tell us what's happens in the application after the user presses a button? what does the button do?

梦一生花开无言 2024-12-21 06:08:07

许多人都想做您正在做的事情,但遗憾的是,这并不是 HTML 的发展方向。在过去一年左右的时间里,你的“小”问题实际上已经成为标准界的一个非常大的问题。例如,请参见 http://www.w3.org/2010/html- 的第 2.4 节xml/快照/

Many people would like to do what you are doing, but sadly, this isn't the way HTML is moving. Your "little" problem has actually been a very big issue in the standards world over the last year or so. See for example section 2.4 of http://www.w3.org/2010/html-xml/snapshot/

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