You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Vinodh Kumar <vi...@gmail.com> on 2011/11/28 08:18:36 UTC

Stripping Contents from XML

Hi,

Can I know how to achieve with ant? Basically I need xml tag content from
[api] <Servers> to [api] </Servers> as output

Any hint?

*      [api] DBTime is 20111128005710
      [api] Invoking api: getServerList, expectedtofail: false
      [api] <?xml version="1.0" encoding="UTF-8"?>
      [api] <Servers>
      [api]     <Server Id="homeid" Name="home"
      [api]         Status="Active" Type="IntegrationAgentServer"/>
       .......
      [api] </Servers>*


to 

* [api] <Servers>
      [api]     <Server Id="homeid" Name="home"
      [api]         Status="Active" Type="IntegrationAgentServer"/>
       .......
      [api] </Servers>*

--Vinodh Kumar


--
View this message in context: http://ant.1045680.n5.nabble.com/Stripping-Contents-from-XML-tp5027993p5027993.html
Sent from the Ant - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: Stripping Contents from XML

Posted by Bruce Atherton <br...@callenish.com>.
If I understand you correctly, you want to take a file that looks to 
contain the output from an Ant run and extract valid XML from it, is 
that correct?

If so, there are two parts you need in a filterchain. The first is 
something to strip the "    [api] " tags from the beginning of each 
line. This is easily done with the replaceregex token filter something 
like the following:

<tokenfilter>
     <replaceregex pattern="^\s*\[api\]" replace="  " flags="i"/>
</tokenfilter>

See 
http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html 
for more details on regular expressions.

The second part is trickier. What you need is something that will remove 
all lines above the first occurrence of a String and below a second 
String. That filter is not currently part of Ant, but should be easy 
enough to implement.

Here is an untested version to give you an idea of what you need:

package org.apache.tools.ant.filters;

import org.apache.tools.ant.types.Parameter;

import java.io.IOException;
import java.io.Reader;

/**
  * Filter which includes only lines within a segment defined by a start 
and end string.
  *
  * Example:
  *
  * <pre>&lt;filterreader 
classname=&quot;org.apache.tools.ant.filters.SegmentFilter&quot;&gt;
  * &lt;param type=&quot;start&quot; value=&quot;foo&quot;/&gt;
  * &lt;param type=&quot;end&quot; value=&quot;bar&quot;/&gt;
  * &lt;/filterreader&gt;</pre>
  *
  * This will include only lines that appear after the string foo 
appears <code>foo</code> and
  * before the string <code>bar</code> appears, including those lines 
containing foo and bar.
  *
  */
public final class SegmentFilter
     extends BaseParamFilterReader
     implements ChainableReader {
     /** Parameter name for the words to filter on. */
     private static final String START_KEY = "start";

     /** Parameter name for the words to filter on. */
     private static final String END_KEY = "end";

     /* State of the filter, either before segment, in segment, or after 
segment */
     private enum STATE {BEFORE, DURING, AFTER}
     private STATE currState = STATE.BEFORE;

     /** Strings that define the start and end of the segment. */
     private String start = "";
     private String end = "";

     /**
      * Remaining line to be read from this filter, or <code>null</code> if
      * the next call to <code>read()</code> should read the original stream
      * to find the next matching line.
      */
     private String line = null;

     /**
      * Constructor for "dummy" instances.
      *
      * @see 
org.apache.tools.ant.filters.BaseFilterReader#BaseFilterReader()
      */
     public SegmentFilter() {
         super();
     }

     /**
      * Creates a new filtered reader.
      *
      * @param in A Reader object providing the underlying stream.
      *           Must not be <code>null</code>.
      */
     public SegmentFilter(final Reader in) {
         super(in);
     }

     /**
      * Returns the next character in the filtered stream, only including
      * lines from the original stream which are contained in the segment.
      *
      * @return the next character in the resulting stream, or -1
      * if the end of the resulting stream has been reached
      *
      * @exception java.io.IOException if the underlying stream throws 
an IOException
      * during reading
      */
     public int read() throws IOException {
         if (!getInitialized()) {
             initialize();
             setInitialized(true);
         }

         int ch = -1;

         if (line != null) {
             ch = line.charAt(0);
             if (line.length() == 1) {
                 line = null;
             } else {
                 line = line.substring(1);
             }
         } else {

             for (line = readLine(); line != null; line = readLine()) {
                 if (currState == STATE.BEFORE) {
                     if (this.start.length() > 0 && 
line.contains(this.start)) {
                         currState = STATE.DURING;
                     }
                 }
                 if (currState == STATE.DURING) {
                     if (this.end.length() > 0 && line.contains(this.end)) {
                         currState = STATE.AFTER;
                     }
                     break;
                 }
             }
             if (line != null) {
                 return read();
             }
         }
         return ch;
     }

     public String getStart() {
         return start;
     }

     public void setStart(String start) {
         this.start = start;
     }

     public String getEnd() {
         return end;
     }

     public void setEnd(String end) {
         this.end = end;
     }

     /**
      * Creates a new LineContains using the passed in
      * Reader for instantiation.
      *
      * @param rdr A Reader object providing the underlying stream.
      *            Must not be <code>null</code>.
      *
      * @return a new filter based on this configuration, but filtering
      *         the specified reader
      */
     public Reader chain(final Reader rdr) {
         SegmentFilter newFilter = new SegmentFilter(rdr);
         newFilter.setStart(getStart());
         newFilter.setEnd(getEnd());
         return newFilter;
     }

     /**
      * Parses the parameters to add user-defined start and end strings.
      */
     private void initialize() {
         Parameter[] params = getParameters();
         if (params != null) {
             for (int i = 0; i < params.length; i++) {
                 if (START_KEY.equals(params[i].getType())) {
                     setStart(params[i].getValue());
                 } else if (END_KEY.equals(params[i].getType())) {
                     setEnd(params[i].getValue());
                 }
             }
         }
     }

}


On 27/11/2011 11:18 PM, Vinodh Kumar wrote:
> Hi,
>
> Can I know how to achieve with ant? Basically I need xml tag content from
> [api]<Servers>  to [api]</Servers>  as output
>
> Any hint?
>
> *      [api] DBTime is 20111128005710
>        [api] Invoking api: getServerList, expectedtofail: false
>        [api]<?xml version="1.0" encoding="UTF-8"?>
>        [api]<Servers>
>        [api]<Server Id="homeid" Name="home"
>        [api]         Status="Active" Type="IntegrationAgentServer"/>
>         .......
>        [api]</Servers>*
>
>
> to
>
> * [api]<Servers>
>        [api]<Server Id="homeid" Name="home"
>        [api]         Status="Active" Type="IntegrationAgentServer"/>
>         .......
>        [api]</Servers>*
>
> --Vinodh Kumar
>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


RE: Stripping Contents from XML

Posted by Brian Agnew <br...@oopsconsultancy.com>.
Check out XMLTask for XML processing in Ant.

http://www.oopsconsultancy.com/software/xmltask

Brian

On Mon, November 28, 2011 13:15, Vinodh Kumar wrote:
> My final output after stripping will be an XML. I tried using regex but
> not
> able to achieve.
>
> --Vinodh Kumar
>
> --
> View this message in context:
> http://ant.1045680.n5.nabble.com/Stripping-Contents-from-XML-tp5027993p5028824.html
> Sent from the Ant - Users mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>
>


-- 
Brian Agnew                  http://www.oopsconsultancy.com
OOPS Consultancy Ltd
Tel: +44 (0)7720 397526
Fax: +44 (0)20 8682 0012


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


RE: Stripping Contents from XML

Posted by Vinodh Kumar <vi...@gmail.com>.
My final output after stripping will be an XML. I tried using regex but not
able to achieve.

--Vinodh Kumar

--
View this message in context: http://ant.1045680.n5.nabble.com/Stripping-Contents-from-XML-tp5027993p5028824.html
Sent from the Ant - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


RE: Stripping Contents from XML

Posted by Emma Burrows <Em...@rpharms.com>.
Hello. Is your input an actual XML file or is it a text file containing all the "[api]" and extra text as indicated?

If it's pure text, you'll probably need to investigate using <filterchain> to remove the unwanted text, using a regex pattern to match all the text you don't want - or all the text you do want - and then exporting the amended result. There is a promising-looking starting point in this thread at Code Ranch (http://www.coderanch.com/t/108154/tools/edit-files-through-ant).


-----Original Message-----
From: Vinodh Kumar [mailto:vinohymi@gmail.com]
Sent: 28 November 2011 07:19
To: user@ant.apache.org
Subject: Stripping Contents from XML

Hi,

Can I know how to achieve with ant? Basically I need xml tag content from
[api] <Servers> to [api] </Servers> as output

Any hint?

*      [api] DBTime is 20111128005710
      [api] Invoking api: getServerList, expectedtofail: false
      [api] <?xml version="1.0" encoding="UTF-8"?>
      [api] <Servers>
      [api]     <Server Id="homeid" Name="home"
      [api]         Status="Active" Type="IntegrationAgentServer"/>
       .......
      [api] </Servers>*


to

* [api] <Servers>
      [api]     <Server Id="homeid" Name="home"
      [api]         Status="Active" Type="IntegrationAgentServer"/>
       .......
      [api] </Servers>*

--Vinodh Kumar


--
View this message in context: http://ant.1045680.n5.nabble.com/Stripping-Contents-from-XML-tp5027993p5027993.html
Sent from the Ant - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org