我正在尝试打开,解析,编辑和输出SVG文档。到目前为止,我已经编辑/更改了nodelist对象的方式,但是,我在保存对SVG文档的更改时遇到了问题(通过序列化)。我怎么能够将我的更改复制/保存到SVG文档并输出到文件。我的目标是更改某些对象(行/路径)上的ID标记,我必须能够找到特定的ID,更改它,然后保存回文件(或新文件)。
这是我的代码(保存到文件):
// Global variables
Logger logger = Logger.getLogger(DynamicLink.class.getSimpleName());
private final File PROJECT_DIRECTORY = new File("").getAbsoluteFile();
private final String PROJECT_PATH = "file.path";
Document doc;
private File svg;
private NodeList nodes;
private final ArrayList<Node> editedLines = new ArrayList();
private void saveSvg() {
try {
File newFile = new File(PROJECT_DIRECTORY, "test/LineTestEdited.svg");
if (!newFile.exists()) {
Files.copy(svg.toPath(), newFile.toPath());
}
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("XML 3.0 LS 3.0");
LSParser parser = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, "http://www.w3.org/TR/REC-xml");
Document document = parser.parseURI(newFile.getPath());
NodeList svgRoot = doc.getChildNodes();
NodeList root = document.getChildNodes();
int length = editedLines.size();
for (int i = 0; i <= length-1; i++) {
Node defaultNode = nodes.item(i+1);
Node newNode = editedLines.get(i);
String defaultName = defaultNode.getNodeName();
String defualtValue = defaultNode.getNodeValue();
String newName = newNode.getNodeName();
String newValue = newNode.getNodeValue();
String type = defaultNode.getBaseURI();
if (newValue != null) {
setNodeValue(newName, defualtValue, newValue, svgRoot);
}
}
LSSerializer serial = impl.createLSSerializer();
LSOutput output = impl.createLSOutput();
output.setEncoding("UTF-8");
output.setByteStream(new FileOutputStream(newFile));
serial.write(document, output);
} catch (Exception ex) {
logger.log(Level.SEVERE, "Error occurred while exporting/saving SVG", ex);
}
}
这是编辑节点列表(editedLines是新更改的节点列表)。其他一切只是加载文档和全局变量。
private void parseSvg() {
int length = nodes.getLength();
int index = 0;
try {
for (int i = 0; i <= length-1; i++) {
Node node = nodes.item(i).cloneNode(true);
String value = node.getNodeValue();
String name = node.getNodeName();
String type = node.getTextContent();
if (!name.equals("svg")) {
if (value != null) {
if (value.contains("Dynamic")) {
node.setNodeValue("Line_" + index);
editedLines.add(node);
index++;
System.out.println("Value: " + node.getNodeValue());
} else {
editedLines.add(node);
}
} else {
editedLines.add(node);
}
}
}
} catch (Exception ex) {
logger.log(Level.SEVERE, "Error occured while parsing SVG", ex);
}
}
使用额外的东西:
private Node getNode(String tagName, String oldId) {
NodeList list = doc.getElementsByTagName(tagName);
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
Element element = (Element)node;
//Element newEle = (Element)element.getElementsByTagName(tagName)
String nodeId = element.getAttribute("id");
if (nodeId.equals(oldId)) {
return node;
}
}
return null;
}
private void setNodeValue(String tagName, String oldId, String id, NodeList nodes) {
Node node = getNode(tagName, oldId);
//Node node =
if ( node == null )
return;
// Locate the child text node and change its value
NodeList childNodes = node.getChildNodes();
for (int y = 0; y < childNodes.getLength(); y++ ) {
Node data = childNodes.item(y);
if ( data.getNodeType() == Node.ATTRIBUTE_NODE ) {
data.setNodeValue(id);
return;
}
}
}