【问题标题】:How can I parse a string to an xml file.For reading the string like an xml?如何将字符串解析为 xml 文件。像 xml 一样读取字符串?
【发布时间】:2017-06-20 14:51:24
【问题描述】:

我有这个字符串:

<dependencies style="typed">
  <dep type="dep">
    <governor idx="1">Maria</governor>
    <dependent idx="2">mrge</dependent>
  </dep>
  <dep type="dep">
    <governor idx="2">mrge</governor>
    <dependent idx="3">la</dependent>
  </dep>
  <dep type="dep">
    <governor idx="1">Maria</governor>
    <dependent idx="4">scoala</dependent>
  </dep>
</dependencies>

我试图通过它,但出现这样的异常,我不知道如何解决它。

这是错误:

3:1: Content is not allowed in prolog.
org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 1; Content is not allowed in prolog.
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at versionTwo.Analyze.convertStringToDocument(Analyze.java:348)
    at versionTwo.Analyze.depRel(Analyze.java:299)
    at versionTwo.MainClass.main(MainClass.java:17)
Exception in thread "main" java.lang.NullPointerException
    at versionTwo.Analyze.depRel(Analyze.java:300)

这是我的代码:

    public String depRel(String graph) throws SAXException, IOException,
            ParserConfigurationException {
        String xmlString;
        xmlString = Features.dependencyGraph(graph);
        String result = "";
        System.out.println("A value og dependency graph is;" + xmlString);
        Document document = parseXmlFromString(xmlString);
        document.getDocumentElement().normalize();
        Element root = document.getDocumentElement();
        NodeList nList = document.getElementsByTagName("dependencies");
        for (int temp = 0; temp < nList.getLength(); temp++) {
            Node node = nList.item(temp);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                // Print each employee's detail
                Element eElement1 = (Element) node;
            }
            NodeList nodesDocPart = node.getChildNodes();
            for (int temp2 = 0; temp2 < nodesDocPart.getLength(); temp2++) {
                Node n = nodesDocPart.item(temp2);
                // /////////////////////////////////////////////////sentence/////////////////////////////////////////////
                NodeList nodesSentencePart = n.getChildNodes();
                for (int temp3 = 0; temp3 < nodesSentencePart.getLength(); temp3++) {
                    Node sentence = nodesSentencePart.item(temp3);
                    if (sentence.getNodeType() == Node.ELEMENT_NODE) {
                        Element eElement4 = (Element) sentence;
                        System.out.println("Sentence : "
                                + eElement4.getTextContent());
                        result = eElement4.getTextContent() + "\n";
                    }
                }
            }
        }
        return result;
    }

    public Document parseXmlFromString(String xmlString)
            throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputStream inputStream = new ByteArrayInputStream(xmlString.getBytes());
        org.w3c.dom.Document document = builder.parse(inputStream);
        return document;
    }

这是我在解析句子后从 XML 创建字符串的方法。我想在另一个类中读取这个字符串,比如 xml,但是我在底部发布的错误出现了。有什么想法吗?

public static String dependencyGraph(String s) {
    Properties props = new Properties();
    props.put("annotators",
            "tokenize, ssplit, pos, lemma, ner, parse, dcoref,depparse");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    Annotation document = new Annotation(s);
    pipeline.annotate(document);
    CoreMap sentence = document.get(
            CoreAnnotations.SentencesAnnotation.class).get(0);
    SemanticGraph dependency_graph = sentence
            .get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class);

    String newLine = System.getProperty("line.separator");
    //convert the output format to a string

    String graph = "\n\nDependency Graph: "
            + dependency_graph.toString(SemanticGraph.OutputFormat.XML)//save the answer like a String from the xml
            + newLine;
    // System.out.println("The graph was made=>" + graph);
    return graph;

}

public static String dependencyGraph(String s) {
    Properties props = new Properties();
    props.put("annotators",
            "tokenize, ssplit, pos, lemma, ner, parse, dcoref,depparse");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    Annotation document = new Annotation(s);
    pipeline.annotate(document);
    CoreMap sentence = document.get(
            CoreAnnotations.SentencesAnnotation.class).get(0);
    SemanticGraph dependency_graph = sentence
            .get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class);

    String newLine = System.getProperty("line.separator");
    //convert the output format to a string

    String graph = "\n\nDependency Graph: "
            + dependency_graph.toString(SemanticGraph.OutputFormat.XML)//save the answer like a String from the xml
            + newLine;
    // System.out.println("The graph was made=>" + graph);
    return graph;

}

【问题讨论】:

标签: java xml string stanford-nlp


【解决方案1】:

在dependencyGraph(String) 中你做的

String graph = "\n\nDependency Graph: "
           + dependency_graph.toString(SemanticGraph.OutputFormat.XML);

创建一个以两个换行符和文本“DependencyGraph”开头的字符串。

然后将其分配给一个变量:

String xmlString;
        xmlString = Features.dependencyGraph(graph);

然后尝试将其解析为 XML:

Document document = parseXmlFromString(xmlString);

但是以两个换行符开头的字符串和文本“依赖关系图”不是格式良好的 XML,因此 XML 解析器抱怨:在第 3 行第 1 列它发现了不能作为 XML 序言的内容文件。

所以标题问题的答案是:如果要将字符串解析为 XML,它必须包含格式正确的 XML。

【讨论】:

  • 非常感谢..确实是这个问题..不行
  • StackOverflow 的惯例是通过将答案标记为已接受来表示感谢(并告诉全世界答案是正确的):单击答案旁边的勾号。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-30
  • 1970-01-01
  • 2014-01-16
  • 2022-01-15
相关资源
最近更新 更多