简体   繁体   中英

How can I copy part of XML to new XML file?

Does anybody know how to copy only one part of XML file into new XML file? I have this:

<?xml version="1.0"?><!DOCTYPE svg  PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 500 401" height="401px" style="stroke: #818181;stroke-width: 1;stroke-linecap: round;stroke-opacity: 0.25;fill: #f4f3f0;" contentStyleType="text/css" zoomAndPan="magnify" preserveAspectRatio="xMidYMid meet" width="500px" enable_background="new 0 0 500 401" contentScriptType="application/ecmascript" version="1.1"><defs></defs><metadata><views><view w="500" zoomAndPan="magnify" preserveAspectRatio="xMidYMid meet" padding="0" h="401"><proj id="laea" lon0="16.693" lat0="45.723"></proj><bbox w="51.2" y="989.82" h="50.8" x="972.4"></bbox><llbbox lon1="180" lon0="-180" lat0="-90" lat1="90"></llbbox></view></views></metadata>
<g>
<path data-name="" d=" " data-fips=""></path>
<path data-name="" d="" data-fips=""></path>
</g>
</svg>

I want to copy everything except data in g tag, how can I do it? Can somebody show me the code?

You're probably looking for XSLT. It's a java technology/library that allows you to define transformations of one xml structure to another. It allows for repeatable, batch operations, and powerful dynamic language that can set variables, use conditional language, etc. You can run it from a command line or from inside of another application.

http://en.wikipedia.org/wiki/XSLT

In your case a simple regular expression replacement should be enough:

final String svmXML = "<?xml....."; // your XML here
final String svmXMlWithoutG = svmXML.replaceAll ("\\<g\\>(?s).*\\</g\\>", ""));

Unless your using CDATA somewhere.

You can achieve this by two ways.

1. XSLT :

Here you can eliminate node <g> thru for-each

2. FileInputStream :

Read the file line by line and write into new file before check line contains string <g> and skip it.

While you could do this with XSLT, here's how you'd do it with just the standard Java APIs (using DOM).

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(/* get an InputStream of your data */);
Element root = document.getDocumentElement();
NodeList children = root.getElementsByTagName("g");
for (int i = 0; i < children.getLength(); ++i) {
    Node child = children.item(i);
    root.removeChild(child);
}
Transformer tr = TransformerFactory.newInstance().newTransformer();
StreamResult result = new StreamResult(/* Get an OutputStream to write to */);
DOMSource source = new DOMSource(document);
tr.transform(source, result);

Doing it this way avoids the brittleness of the regular expressions (which will work for this specific case, but if you need to start getting fancier, they'll get out of hand very quickly with XML), and reduces the assumptions you have to make about the way your input is formatted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM