使用krpano的krpanotools生成场景列表后,得到tour.xml,可以看到文件内容是这样的(这里只展示场景列表的内容,name和title是自定义修改后的):
<scene name="scene_153569621085135" title="123" onstart="" thumburl="panos/153569621085135.tiles/thumb.jpg" lat="" lng="" heading="">
<view hlookat="0.0" vlookat="0.0" fovtype="MFOV" fov="120" maxpixelzoom="2.0" fovmin="70" fovmax="140" limitview="auto"/>
<preview url="panos/153569621085135.tiles/preview.jpg"/>
<image>
<cube url="panos/153569621085135.tiles/pano_%s.jpg"/>
</image>
</scene>
<scene name="scene_153569622468835" title="123" onstart="" thumburl="panos/153569622468835.tiles/thumb.jpg" lat="" lng="" heading="">
<view hlookat="0.0" vlookat="0.0" fovtype="MFOV" fov="120" maxpixelzoom="2.0" fovmin="70" fovmax="140" limitview="auto"/>
<preview url="panos/153569622468835.tiles/preview.jpg"/>
<image>
<cube url="panos/153569622468835.tiles/pano_%s.jpg"/>
</image>
</scene>
<scene name="scene_153569623181035" title="345" onstart="" thumburl="panos/153569623181035.tiles/thumb.jpg" lat="" lng="" heading="">
<view hlookat="0.0" vlookat="0.0" fovtype="MFOV" fov="120" maxpixelzoom="2.0" fovmin="70" fovmax="140" limitview="auto"/>
<preview url="panos/153569623181035.tiles/preview.jpg"/>
<image>
<cube url="panos/153569623181035.tiles/pano_%s.jpg"/>
</image>
</scene>
<scene name="scene_153569623879735" title="567567" onstart="" thumburl="panos/153569623879735.tiles/thumb.jpg" lat="" lng="" heading="">
<view hlookat="0.0" vlookat="0.0" fovtype="MFOV" fov="120" maxpixelzoom="2.0" fovmin="70" fovmax="140" limitview="auto"/>
<preview url="panos/153569623879735.tiles/preview.jpg"/>
<image>
<cube url="panos/153569623879735.tiles/pano_%s.jpg"/>
</image>
</scene>
用Java解析XML并进行删除操作
private boolean removeScene4XML(String name, HttpServletRequest request, Long vrId) throws Exception {
String realPath = new File(request.getSession().getServletContext().getRealPath("/")).getParent();
String xmlPath = realPath + "/vrFiles/vr/" + vrId + "/vtour/tour.xml"; //找到自己的XML文件路径
String backupPath = realPath + "/vrFiles/vr/" + vrId + "/vtour/tour_tmp.xml"; //这个是备份路径,这里用不上可以不用理
try {
// 删除操作
File XmlFile = new File(xmlPath);
SAXReader saxReader = new SAXReader();
Document doc = saxReader.read(XmlFile);
List<Element> list = doc.selectNodes("/krpano"); //找到根节点
for (Element element : list) {
Iterator it = element.elementIterator("scene"); //找到场景节点
while (it.hasNext()) {
Element es = (Element) it.next();
if(Utils.isNotEmpty(es.attributeValue("name")) && es.attributeValue("name").equals("scene_"+name)) { //匹配传进来的场景名称
element.remove(es); //删除该节点
}
}
}
// 输出格式
OutputFormat outformat = new OutputFormat();
// 指定XML编码
outformat.setEncoding("UTF-8");
outformat.setNewlines(true);
outformat.setIndent(true);
outformat.setTrimText(true);
OutputStream out = new FileOutputStream(XmlFile);
XMLWriter xmlwriter = new XMLWriter(out, outformat);
xmlwriter.write(doc);
xmlwriter.close();
return true;
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return false;
}
这样就可以删除tour.xml中自己想要删除的节点了。
MoonJo笔记