abbisDK
abbisDK

Reputation: 35

remove every other string entry

I have collected som data from an XML file and I am now trying to edit the data so that it fits to my program. The data which I collect state the minimum and maximum value of a given parameter, however I am only interested in either the minimum or maximum values.

The XML data source looks like:

<tolerated>
    <UMIN2> [ 181.2 186.8 ] </UMIN2>
    <T_UMIN2> [ 0.4 0.6 ] </T_UMIN2>
    <UMIN1> [ 197 203 ] </UMIN1>
    <T_UMIN1> [ 2.4 2.6 ] </T_UMIN1>
    <UMAX1> [ 249.2 256.8 ] </UMAX1>
    <T_UMAX1> [ 0.9 1.1 ] </T_UMAX1>
    <UMAX2> [ 260 268 ] </UMAX2>
    <T_UMAX2> [ 0.4 0.6 ] </T_UMAX2>
</tolerated>

I have gotten my program to load this data into a string called tol, via this code

public static String tolerance() throws SAXException, IOException, ParserConfigurationException, XPathExpressionException{
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(O1.replace("\\","\\\\"));
        XPathFactory xpf = XPathFactory.newInstance();
        XPath xpath = xpf.newXPath();

        String tTemp = new String();

        XPathExpression expr1 = xpath.compile("ptfGen/body/gridCode/"+header2.substring(31,header2.length()-23)+"/tolerated");
        Node nodeGettingChanged1 = (Node) expr1.evaluate(doc, XPathConstants.NODE);

        String toldata = new String();

        NodeList childNodes1 = nodeGettingChanged1.getChildNodes();
        for (int i = 0; i != childNodes1.getLength(); i++)
        {
            Node child = (Node) childNodes1.item(i);
            if (!(child instanceof Element))
                continue;

            if (child.getNodeName().equals("tolerated"));{
                toldata = child.getFirstChild().getNodeValue();

                tTemp += toldata.substring(3,toldata.length()-3).replace(" ", "\n") + "\n";
            }
            tol = tTemp;
        }
       return tTemp;
    }

This gives me all the values with a newline separation.

My question is now can I/how can I remove every other entry so that I only have either the minimum or maximum values?´

the output I am getting now is something like

tol = {"181.2\n186.8\n0.4\n0.6\n197\n.........."};

and what I amm looking for is

tol = {"181.2\n0.4\n197\n.........."};

Upvotes: 0

Views: 136

Answers (2)

Mister Smith
Mister Smith

Reputation: 28158

I'd do the following:

  1. Define a class to hold temps:

    public static class TempEntry {
        private final String min, max;
    
        public TempEntry(min, max){
            this.min = min;
            this.max = max;
        }
    
        public String getMin(){
            return min;
        }
    
        public String getMax(){
            return max;
        }
    }
    
  2. When parsing, split the text in the nodes and create a new TempEntry

  3. The parser could return either an immutable List<TempEntry> or a TempEntry[] array.

  4. Now create a method or a new class that iterates through the list and pretty-prints it based on parameters (only mins, only max, both, etc)

This way you don't couple your parsing logic to your presentation logic (which may change more frequently).

Upvotes: 0

Bernhard Barker
Bernhard Barker

Reputation: 55589

If the minimum is always first and the maximum always second.

All you need to do is change

tTemp += toldata.substring(3,toldata.length()-3).replace(" ", "\n") + "\n";

to

tTemp += toldata.substring(3,toldata.length()-3).split(" ")[c] + "\n";

where c is 0 for the first (minimum) and 1 for the second (maximum).

or a simpler

tTemp += toldata.split(" ")[c] + "\n";

where c is 2 or 3 in the respective cases.

This of course assumes the string is always in the exact form: " [ number number ] " (always with exactly the same amount of spaces in the exact same positions). A simple regular expression of "\\s+" instead of " " will allow one or more spaces instead, i.e.:

tTemp += toldata.split("\\s+")[c] + "\n";

\s means white-space and + means one or more. The additional \ is to escape the \ for the compiler.

Upvotes: 1

Related Questions