2

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?

kiritsuku
  • 52,967
  • 18
  • 114
  • 136
njamanjam
  • 245
  • 2
  • 3
  • 9

4 Answers4

0

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

chad
  • 7,369
  • 6
  • 37
  • 56
0

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.

ShyJ
  • 4,560
  • 1
  • 19
  • 19
  • how can he have large xml as string ? – vels4j Nov 15 '12 at 18:04
  • @Stivel Sure. The limit on the length of `String` is quite [high](http://stackoverflow.com/questions/816142/strings-maximum-length-in-java-calling-length-method). It must of course fit into memory. – ShyJ Nov 15 '12 at 21:25
0

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.

vels4j
  • 11,208
  • 5
  • 38
  • 63
0

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.

Ian McLaird
  • 5,507
  • 2
  • 22
  • 31