DTD 的数据类型定义中有ID 标志
不知是否?

解决方案 »

  1.   

    Document.getElementById
    Text in the first DIV. 
    Some text in the second DIV. 
    Some text and element in the third DIV. 
    We can try another elements. It will be much more interesting. 
    Text in the last DIV. 
    Source:        <div id="doc">
             <div>
               Text in the first DIV.
             </div>
             <div id="DDD" class="secondClass">
               Some text in the second DIV.
             </div>
             <div class="thirdClass">
               Some text and <span id="SSS">element</span> in the third DIV.
             </div>
             <div class="fourthClass">
               We can try <i>another elements</i>.
               It will be much more <b>interesting</b>.
             </div>
             <div>
               Text in the last DIV.
             </div>
           </div>
         
     
    JavaScript:   var output1 = document.getElementById('doc').nodeName;
      var output2 = document.getElementById('SSS').nodeName; 
    Output: desired your browser 
    output1: DIV
    output2: SPAN  output1: DIV
    output2: SPAN
     
     
      

  2.   

    这个是JS的例子
    不过在XML里面也差不多
      

  3.   

    在我的程序中用到了DOM接口,程序如下:
         先声明
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         DocumentBuilder db = dbf.newDocumentBuilder();
         Document doc = db.parse(new File("c:\\test3.xml"));
         然后想
         Element student = doc.getElementById("001");我就希望通过元素的ID来取到某个Element,现在就是不知道怎么去设这个ID,上面的说法我也看到过,不过好像和我提的问题不到一样。
      

  4.   

    <?xml version='1.0' encoding='utf-8'?>
    <sample:sample xmlns:sample="http://company.com/sample">
      <sample:elem attr="a" id="e1"> 1
        <sample:elem attr="b" id="e2"> 1 2
          <sample:elem attr="c" id="e3"> 1 2 3
          </sample:elem>
        </sample:elem>
      </sample:elem>
      <array id="e4">
        <elem attr="a" id="e5"> 1 </elem>
        <elem attr="b" id="e6"> 2 </elem>
        <elem attr="c" id="e7"> 3 </elem>
        <elem attr="a" id="e8"> 4 </elem>
        <elem attr="b" id="e9"> 5 </elem>
      </array>
    </sample:sample>
    这样设置
    id
      

  5.   

    这种方法我已经试过啦,这只是在XML文档里给每个元素设了一个叫id的属性,但DOM对象解析文档时并不知道哪个属性作为元素的id,并不是就直接认名为id的属性为元素的id,我所请教的正是这一点。
      

  6.   

    sorry啊,终于明白你的意思了~~
    那个问题,正如rex0y(的开发绝望) 所言
    要自己设置,下面给你个例子:比如XML文档是这样的
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE invoice SYSTEM "invoice.dtd">
    <invoice number="1234567" month="2" day="20" year="2000" id="e4">
      <supplier name="Sup Company">
        <email>[email protected]</email>
        <address><![CDATA[London, UK]]></address>
      </supplier>
      <buyer name="Buy Company">
        <email>[email protected]</email>
        <address><![CDATA[Paris, France]]></address>
      </buyer>
      <currency>US&dollar;</currency>
      <item name="Computer" quantity="10" price="1000"
        info="800 Mhz, 256M RAM, 40G HDD"/>
      <item name="Printer" quantity="5" price="200"
        info="600x600 dpi, 20 ppm"/>
      <item name="Server" quantity="1" price="5000"
        info="4x1000 Mhz, 2G RAM, 2x100G HDD"/>
      <?total?>
    </invoice>
    里面
    <invoice number="1234567" month="2" day="20" year="2000" id="e4">
    这个地方有id这个属性
    不过你要在dtd里面这样设置一下
    <?xml version="1.0" encoding="UTF-8"?><!-- External DTD --><!ELEMENT invoice (supplier, buyer, currency, item*)>
    <!ATTLIST invoice number CDATA #REQUIRED>
    <!ATTLIST invoice month  CDATA #REQUIRED>
    <!ATTLIST invoice day    CDATA #REQUIRED>
    <!ATTLIST invoice year   CDATA #REQUIRED>
    <!ATTLIST invoice id     ID    #REQUIRED><!ELEMENT supplier (email, address)>
    <!ATTLIST supplier name CDATA #REQUIRED><!ELEMENT buyer (email, address)>
    <!ATTLIST buyer name CDATA #REQUIRED><!ELEMENT email (#PCDATA)><!ELEMENT address (#PCDATA)><!ELEMENT currency (#PCDATA)><!ELEMENT item EMPTY>
    <!ATTLIST item name     CDATA  #REQUIRED>
    <!ATTLIST item quantity CDATA  #REQUIRED>
    <!ATTLIST item price    CDATA  #REQUIRED>
    <!ATTLIST item info     CDATA  #REQUIRED><!ENTITY dollar "$">
    也就是在这个地方
    <!ATTLIST invoice id     ID    #REQUIRED>
    要设置id的属性为ID然后就可以在程序里面用
     Element e4 = doc.getElementById( "e4" );
    来访问了现在行了吗?