I have this line from an XML document:
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" contentScriptType="text/ecmascript" width="1024" zoomAndPan="magnify" contentStyleType="text/css" viewBox="0 0 1024 768" height="768" preserveAspectRatio="xMidYMid meet" version="1.0">
I want to be able to split it up, using the split method. For example i want to save each parameter into a String array.
So i'd like:
contentScriptType="text/ecmascript"
width="1024"
zoomAndPan="magnify"
contentStyleType="text/css"
viewBox="0 0 1024 768"
height="768"
etc etc to be saved into a string array, is there anyway to do this using the split method, or can anybody suggest an easier, more efficient way to do this?
Here is the scary looking regular expression:
\s(.*?)\s?=(?:(?:\\[,"']|[^,"'])+|"(?:\\"|[^"])*(?<!\\)"|'[^']*'|)
Eclipse wont accept this as it has invalid character constants, anybody know how to overcome this error?
If you for some reason don't want to use Sax (which I would suggest too), the reason that Eclipse is rejecting your regular expression is that you have to escape \ in the pattern and " in the String literal. So you pattern string definition should look like:
There are multiple ways to represent the same XML document (see below), differences in white space and quotes can make it difficult to write (and maintain) a regular expression.
input.xml (representation 1)
input.xml (representation 2)
I would recommend using an XML parser. Below is how it could be done using StAX (JSR-173). An implementation of a StAX parser is included in Java SE 6.
Demo
Output
Read it with DOM or SAX, process the attributes and add it to a map.