gpt4 book ai didi

java - 使用 JSoup 编写 XML

转载 作者:行者123 更新时间:2023-11-30 03:31:44 27 4
gpt4 key购买 nike

我已经使用 JSoup 解析了 xml 文件,现在我想将(修改后的)对象写入新的 xml 文件。

问题在于 JSoup 添加了一堆元头 html 数据。

它应该这样开始:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE score-partwise PUBLIC "-//Recordare//DTD MusicXML 2.0 Partwise//EN" "http://www.musicxml.org/dtds/partwise.dtd">
<score-partwise>
<identification>
<encoding>

但它实际上是这样开始的:

<!--?xml version="1.0" encoding="UTF-8"?--><!DOCTYPE score-partwise PUBLIC "-//Recordare//DTD MusicXML 2.0 Partwise//EN" "http://www.musicxml.org/dtds/partwise.dtd">
<html>
<head></head>
<body>
<score-partwise>
<identification>
<encoding>
<software>
MuseScore 1.3
</software>
<encoding-date>
2015-01-31
</encoding-date>
</encoding>
<source>http://musescore.com/score/161981
</identification>
<defaults>
<scaling>
<millimeters>
7.056
</millimeters>
<tenths>
40
</tenths>
</scaling>
<page-layout>
<page-height>
1683.67
</page-height>
<page-width>
1190.48
</page-width>

我已经加载了这样的文件:

 if (doc.getElementsByTag("note").isEmpty()) {
doc = Jsoup.parse(input, "UTF-16", filename);
if (doc.getElementsByTag("note").isEmpty()) {
System.out.println("Please check that your file is encoded in UTF-8 or UTF-16 and contains notes.");
}
}

并尝试这样写:

BufferedWriter htmlWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("output.xml"), "UTF-8"));
htmlWriter.write(doc.outerHtml());

-> 我也尝试过 doc.html() 和 doc.toString() 。仍然是相同的输出。

有什么想法吗?我只是希望它的书写方式与阅读方式相同。

最佳答案

这解决了它:

InputStream is = new FileInputStream(filename) {
@Override
public int read() throws IOException {
return 0;
}
};

doc = Jsoup.parse(is, "UTF-8", "", Parser.xmlParser());


if (doc.getElementsByTag("note").isEmpty()) {
doc = Jsoup.parse(is, "UTF-8", "", Parser.xmlParser());
if (doc.getElementsByTag("note").isEmpty()) {
System.out.println("Please check that your file is encoded in UTF-8 or UTF-16 and contains notes.");
}
}

关于java - 使用 JSoup 编写 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28865048/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com