私はこの文字列があります。文字列をXMLファイルに解析するにはどうすればよいですか?xmlのような文字列を読み取るには?
<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;
}
そして、これはsentence.This文字列Iを解析した後、XMLから文字列を作成し、私の方法であり、 XMLのような別のクラスを読み込みたいのですが、下に投稿したエラーが表示されます。 dependencyGraph(文字列)で
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;
}
[コンテンツはProlog SAXParserExceptionで許可されていません](https://stackoverflow.com/questions/4569123/content-is-not-allowed-in-prolog-saxparserexception) – tnw