问题发生在解析XML的时候,出来的数字总是两倍的,比如,明明只有13个角色,为什么的在JAVA的代码中却出现了26个角色?而其他的都是正确的。 doc.getDocumentElement().normalize();
// parser participants
Element root = doc.getDocumentElement();
System.out.println("Root element of the doc is " + doc.getDocumentElement().getNodeName());

NodeList listofParticipant  =  doc.getElementsByTagName("participant");
NodeList listofRoles =  doc.getElementsByTagName("role");
NodeList listofOrggroups = doc.getElementsByTagName("orggroup");

System.out.println("the number of participants is " + listofParticipant.getLength());
                           // 这一个出来的总是 26 个
System.out.println("the number of roles is " + listofRoles.getLength());
System.out.println("the number of orggroups is " + listofOrggroups.getLength());

解决方案 »

  1.   

    那只能说明你的XML文件里确实有26个角色
      

  2.   

    把你的xml贴出来啊啊啊啊啊啊啊啊(没办法,说我回复内容太短了!)
      

  3.   


    <?xml version="1.0" encoding="UTF-8"?>
    <orgdata>
      <participants>
        <participant id="PA-89be9dfd-b389-4550-8170-aba4930abc36">
          <userid>exec_producer</userid>
          <password>bUlBkHMDhGQMFWNZUYdBGwmcoqQ=</password>
          <firstname>Alan</firstname>
          <lastname>Lee (executive producer)</lastname>
          <description />
          <notes />
          <isAdministrator>false</isAdministrator>
          <isAvailable>true</isAvailable>
          <roles>
            <role>RO-55ed22a1-b494-42b7-9103-c16ce4f9b46d</role>
          </roles>
          <positions />
          <capabilities />
          <privileges>00000000</privileges>
        </participant>
        <participant id="PA-b7a79138-158d-4fce-b875-61029bb85431">
          <userid>prod-coordinator</userid>
          <password>bUlBkHMDhGQMFWNZUYdBGwmcoqQ=</password>
          <firstname>Florian</firstname>
          <lastname>Lang (production coordinator)</lastname>
          <description />
          <notes />
          <isAdministrator>false</isAdministrator>
          <isAvailable>true</isAvailable>
          <roles>
            <role>RO-9b1bf293-9c1e-4868-8df0-f1f1911f5fca</role>
          </roles>
          <positions />
          <capabilities />
          <privileges>00000000</privileges>
        </participant>
        <participant id="PA-6477f285-8b81-4b01-a2f1-9e5be1670810">
          <userid>prod-manager</userid>
          <password>bUlBkHMDhGQMFWNZUYdBGwmcoqQ=</password>
          <firstname>Alice</firstname>
          <lastname>White (production manager)</lastname>
          <description>Alice White is a third-year student with a major of film production management. After she gets her degree, she wants to be a production manager of feature films and TV series.</description>
          <notes>Contact details:
    Address - 101 Pacific Road, Ocean City 
    Email - [email protected]</notes>
          <isAdministrator>true</isAdministrator>
          <isAvailable>true</isAvailable>
          <roles>
            <role>RO-7cfc01ca-7397-4d13-acc3-104a2c6ea3b0</role>
          </roles>
          <positions />
          <capabilities />
          <privileges>10000100</privileges>
        </participant>
        <participant id="PA-73158bc9-072d-4dec-9cb1-323698c1e276">
          <userid>cam-assistant</userid>
          <password>Se4tMaQCi9gr0Q2usp7P56Sk5vM=</password>
          <firstname>Thomas</firstname>
          <lastname>Raymond (camera assistant)</lastname>
          <description />
          <notes />
          <isAdministrator>false</isAdministrator>
          <isAvailable>true</isAvailable>
          <roles>
            <role>RO-d00eaa77-0fe7-44c6-916e-c0dbc5710baf</role>
          </roles>
          <positions />
          <capabilities />
          <privileges>00000000</privileges>
        </participant>
        <participant id="PA-17fde9e1-0f4f-4abd-9eeb-967078ce8a64">
          <userid>director</userid>
          <password>bUlBkHMDhGQMFWNZUYdBGwmcoqQ=</password>
          <firstname>Melvin</firstname>
          <lastname>Montalban (director)</lastname>
          <description />
          <notes />
          <isAdministrator>false</isAdministrator>
          <isAvailable>true</isAvailable>
          <roles>
            <role>RO-3fd11c87-c95c-40cc-bbd7-ed0690420354</role>
          </roles>
          <positions />
          <capabilities />
          <privileges>00000000</privileges>
        </participant>
     </participants>
      <roles>
        <role id="RO-d2e982aa-96b7-40df-9b27-e78a5cb0c73b">
          <name>1st AD</name>
          <description />
          <notes />
        </role>
        <role id="RO-d0e1e1d0-6413-433f-8043-56c821686eaa">
          <name>2nd AD</name>
          <description />
          <notes />
        </role>
        <role id="RO-d00eaa77-0fe7-44c6-916e-c0dbc5710baf">
          <name>Camera Assistant</name>
          <description />
          <notes />
        </role>
        <role id="RO-5cf6afa6-36cd-4398-890f-829ceaa282b4">
          <name>Continuity</name>
          <description />
          <notes />
        </role>
        <role id="RO-3fd11c87-c95c-40cc-bbd7-ed0690420354">
          <name>Director</name>
          <description />
          <notes />
        </role>
      </roles>
      <positions />
      <capabilities />
      <orggroups>
        <orggroup id="OG-200b6b20-5023-47b7-8ef9-ad1ec55f87ff">
          <groupName>Production Office</groupName>
          <groupType>GROUP</groupType>
          <description />
          <notes />
        </orggroup>
      </orggroups>
    </orgdata>
      

  4.   

    只贴了其中的一部分,participant和role都是重复出现的,我数了,participants和roles下面都是13个participant 和 role, participant是正确的13个, role却出现了26个,另外,capabilities和positions都出现了14个
      

  5.   

    其实原因很简单,participant里面也有role,Doc计算的是所有role节点数,所以Role的个数是上面participant里面的Role加上下面的Role。
      

  6.   

    <participants><participant><roles><role>...</role></roles></participant></participants>
    <roles><role>...</role></roles>没错啊。。role节点在两个层次分别出现了
      

  7.   

    把listofRoles的内容打出来看看就知道了
      

  8.   

    哦,谢谢,再问几个问题1 participants 下面的 node 都是 participant,我怎么才能得到一个 list 里面包含的都是 participant呢? 以前似乎做过一次,但是得到都是双倍的数字,打印出来是
    #text
    particiapnt2 如果不修改文件的情况下,怎么能够避免这种重复出现的情况,也就是在 participant 下出现的 role都不计入3 一般来讲,一个xml都会有个DTD或者XSD,在工作中,一般是怎么用的呢?有没有读入了XSD后,就能够解析出xml中有多少个A 多少个B4 想这个例子中又相互包含的情况,怎么理清楚这个关系呢?