0

我有一组来自自然语言工具的 XML 字符串输出,需要从中检索值,还为输出字符串中未显示的那些标签提供空值。尝试使用使用 Java从 XML 中提取数据中提供的 Java 代码,但它似乎不起作用。

当前样品标签库存如下:

<TimeStamp>, <Role>, <SpeakerId>, <Person>, <Location>, <Organization> 

示例 XML 输出字符串:

<TimeStamp>00.00.00</TimeStamp> <Role>Speaker1</Role><SpeakerId>1234</SpeakerId>Blah, blah, blah. 

愿望输出:

TimeStamp: 00.00.00
Role: Speaker1
SpeakerId: 1234
Person: null
Place: null
Organization: null

为了使用上面链接中提供的 Java 代码(在更新的代码中),我插入<Dummy>如下</Dummy>

<Dummy><TimeStamp>00.00.00</TimeStamp><Role>Speaker1</Role><SpeakerId>1234</SpeakerId>Blah, blah, blah.</Dummy>

但是,它只返回 dummy 和 null。由于我仍然是Java的新手,因此非常感谢详细的解释。

4

2 回答 2

0

试试这种方式:D希望可以帮助你

File fXmlFile = new File("yourfile.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);

您可以像这样获取子节点列表:

NodeList nList = doc.getElementsByTagName("staff");

像这样获取项目:

Node nNode = nList.item(temp);

示例网站

于 2013-06-05T02:54:35.170 回答
0

这就是我最终为我的 Java 包装器所做的(仅显示时间戳)

  public class NERPost {

      public String convertXML (String input) {
      String nerOutput = input;
      try {
           DocumentBuilderFactory docBuilderFactory = 
           DocumentBuilderFactory.newInstance();
           DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
           InputSource is = new InputSource();            
           is.setCharacterStream(new StringReader(nerOutput));       
           Document doc = docBuilder.parse(is);

        // normalize text representation
        doc.getDocumentElement ().normalize ();
        NodeList listOfDummies = doc.getElementsByTagName("dummy");


        for(int s=0; s<listOfDummies.getLength() ; s++){
            Node firstDummyNode = listOfDummies.item(s);
            if(firstDummyNode.getNodeType() == Node.ELEMENT_NODE){
               Element firstDummyElement = (Element)firstDummyNode;

         //Convert each entity label --------------------------------

          //TimeStamp
               String ts = "<TimeStamp>";
               Boolean foundTs;

               if (foundTs = nerOutput.contains(ts)) {                    
           NodeList timeStampList = firstDummyElement.getElementsByTagName("TimeStamp");

          //do it recursively  
                for (int i=0; i<timeStampList.getLength(); i++) {       
                Node firstTimeStampNode = timeStampList.item(i);
                Element timeStampElement = (Element)firstTimeStampNode;
                NodeList textTSList = timeStampElement.getChildNodes();
                String timeStampOutput = ((Node)textTSList.item(0)).getNodeValue().trim();
                System.out.println ("<TimeStamp>" + timeStampOutput + "</TimeStamp>\n")
                   } //end for
                }//end if
             //other XML tags
              //.....
               }//end if
              }//end for
           }
            catch...
              }//end try
                }}
于 2013-08-03T22:24:16.883 回答