无法从XML文件中提取时间数据

时间:2017-01-22 19:01:05

标签: java xml javafx scenebuilder

我正在尝试编写一个软件,它将有一个视频播放器,成绩单并同步运行它们。

目前我的成绩单存在问题。我附上了我正在使用的XML文件:

NodeList nodeParagraphs = root.getElementsByTagName("u");
NodeList nodeParagraphs2 = root.getElementsByTagName("internal-media");
  for(int i=0; i < nodeParagraphs.getLength(); i++){
    Element nodeParagraph = (Element)nodeParagraphs.item(i); 
    Element nodeParagraph2 = (Element)nodeParagraphs2.item(i);
    String id = nodeParagraph.getAttribute("uID");
    String who = nodeParagraph.getAttribute("who");
    String Time = nodeParagraph2.getAttribute("start");
    Paragraph p = new Paragraph(who, id, Time);

    NodeList wNodeList = nodeParagraph.getElementsByTagName("w");
    for(int j=0; j < wNodeList.getLength(); j++){
      Element wElem = (Element)wNodeList.item(j);
      String word = wElem.getTextContent();
      p.addWord(word);
    }
    chat.addParagraph(p);
  }

问题是当我显示成绩单时,他们在错误的时间打印,因为每个u部分都有多个内部媒体标签。当我只需要每个段落的第一个时,它会占用所有这些。示例如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<CHAT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://www.talkbank.org/ns/talkbank"
      xsi:schemaLocation="http://www.talkbank.org/ns/talkbank http://talkbank.org/software/talkbank.xsd"
      Media="future" Mediatypes="video"
      PID="11312/t-00017262-1"
      Font="CAfont:13:0"
      Version="2.2.1"
      Lang="eng"
      Options="CA"
      Corpus="DaCapo"
      Date="1984-01-01">
  <Participants>
    <participant
      id="DAC"
    name="Dacapo_Leader"
      role="Adult"
      language="eng"

    />
    <participant
      id="MIC"
    name="Michael"
      role="Adult"
      language="eng"

    />
    <participant
      id="LUI"
    name="Luis"
      role="Adult"
      language="eng"

    />
    <participant
      id="NIN"
    name="Nina"
      role="Adult"
      language="eng"

    />
    <participant
      id="KEN"
      role="Adult"
      language="eng"

    />
    <participant
      id="JAK"
    name="Jakob"
      role="Adult"
      language="eng"

    />
    <participant
      id="XXX"
      role="Unidentified"
      language="eng"

    />
    <participant
      id="WOM"
    name="Dacapo_Woman"
      role="Adult"
      language="eng"

    />
  </Participants>
  <u who="KEN" uID="u0">
    <w>as</w>
    <w>it</w>
    <w>currently</w>
    <w>stands</w>
    <w>one</w>
    <w>of</w>
    <w>the</w>
    <w>things</w>
    <w>that</w>
    <w>people</w>
    <w>do</w>
    <internal-media
      start="0.000"
      end="2.520"
      unit="s"
    />
    <w>is</w>
    <w>create</w>
    <internal-media
      start="2.520"
      end="3.240"
      unit="s"
    />
    <w>one</w>
    <w>of</w>
    <w>the</w>
    <w>things</w>
    <w>that</w>
    <w>anthropologists</w>
    <w>design</w>
    <w>researchers</w>
    <w>do</w>
    <internal-media
      start="3.240"
      end="6.720"
      unit="s"
    />
    <w>is</w>
    <w>they</w>
    <w>create</w>
    <w>distance</w>
    <w>between</w>
    <w>business</w>
    <w>people</w>
    <internal-media
      start="6.720"
      end="9.160"
      unit="s"
    />
    <w>and</w>
    <w>uh</w>
    <t type="missing CA terminator"></t>
    <media
      start="9.160"
      end="11.200"
      unit="s"
    />
  </u>
  <u who="DAC" uID="u1">
    <w>participants</w>
    <t type="missing CA terminator"></t>
    <media
      start="11.200"
      end="11.800"
      unit="s"
    />
  </u>

1 个答案:

答案 0 :(得分:0)

你的逻辑错了。首先,将整个文档中的所有<internal-media>元素收集到一个列表中。其次,使用在另一个不同大小的列表上使用的相同索引变量迭代该列表。

您需要每次为当前<u>元素的后代元素构建“内部媒体”列表。之后,只需从列表

中获取第一个(0索引)项
NodeList nodeParagraphs = root.getElementsByTagName("u");
  for(int i=0; i < nodeParagraphs.getLength(); i++){
    Element nodeParagraph = (Element)nodeParagraphs.item(i); 
    NodeList internalMediaList = nodeParagraph.getElementsByTagName("internal-media");
    Element firstInternalMedia = (Element)internalMediaList.item(0);
    String time = firstInternalMedia.getAttribute("start");

对于(无论你崇拜的人)的爱,使用有意义的,适当的变量名称......