XMLfile parsher


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting XMLfile parsher
# 1  
Old 09-27-2004
Data XMLfile parsher

Hi all
Does anyone know how I can parse an xml file and get only some data? For example:
<?xml version="1.0"?>
<!--?xml-stylesheet type="text/xsl" href="irpexamle.xsl"?-->
<mdc>
<md>
<neid>
<neun></neun>
<nedn></nedn>
</neid>
<mi>
<mts>20040521120243</mts>
<gp>161</gp>
<mt>AlarmQueueNumOfThreads</mt>
<mt>BillingQueueNumOfThreads</mt>
<mt>EventQueueNumOfThreads</mt>
<mt>OperatorQueueNumOfThreads</mt>
<mt>SosQueueNumOfThreads</mt>
<mv>
<moid>ler2.operator.net</moid>
<r>1</r>
<r>2</r>
<r>3</r>
<r>4</r>
<r>5</r>
<sf>false</sf>
</mv>
</mi>
</md>
</mdc>
AND THE OUTPUT TO BE:
<mt>AlarmQueueNumOfThreads</mt> <r>1</r>
<mt>BillingQueueNumOfThreads</mt> <r>2</r>
.....
.....
.....
<mt>SosQueueNumOfThreads</mt> <r>5</r>
# 2  
Old 09-30-2004
Have you tried awk? There is an option to search for strings and the text in-between those strings. Read the man page.

awk /"AlarmQueueNumOfThreads"/,/SosQueueNumOfThreads"/
# 3  
Old 10-01-2004
Here is a quickstart SAX program taken from
http://www.saxproject.org

It reads all characters, so you could modify it to fit your
needs.

Code:
import java.io.FileReader;

import org.xml.sax.XMLReader;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.helpers.DefaultHandler;


public class MySAXApp extends DefaultHandler
{

    public static void main (String args[])
	throws Exception
    {
	XMLReader xr = XMLReaderFactory.createXMLReader();
	MySAXApp handler = new MySAXApp();
	xr.setContentHandler(handler);
	xr.setErrorHandler(handler);

				// Parse each file provided on the
				// command line.
	for (int i = 0; i < args.length; i++) {
	    FileReader r = new FileReader(args[i]);
	    xr.parse(new InputSource(r));
	}
    }


    public MySAXApp ()
    {
	super();
    }


    ////////////////////////////////////////////////////////////////////
    // Event handlers.
    ////////////////////////////////////////////////////////////////////


    public void startDocument ()
    {
	System.out.println("Start document");
    }


    public void endDocument ()
    {
	System.out.println("End document");
    }


    public void startElement (String uri, String name,
			      String qName, Attributes atts)
    {
	if ("".equals (uri))
	    System.out.println("Start element: " + qName);
	else
	    System.out.println("Start element: {" + uri + "}" + name);
    }


    public void endElement (String uri, String name, String qName)
    {
	if ("".equals (uri))
	    System.out.println("End element: " + qName);
	else
	    System.out.println("End element:   {" + uri + "}" + name);
    }


    public void characters (char ch[], int start, int length)
    {
	System.out.print("Characters:    \"");
	for (int i = start; i < start + length; i++) {
	    switch (ch[i]) {
	    case '\\':
		System.out.print("\\\\");
		break;
	    case '"':
		System.out.print("\\\"");
		break;
	    case '\n':
		System.out.print("\\n");
		break;
	    case '\r':
		System.out.print("\\r");
		break;
	    case '\t':
		System.out.print("\\t");
		break;
	    default:
		System.out.print(ch[i]);
		break;
	    }
	}
	System.out.print("\"\n");
    }

}

Login or Register to Ask a Question

Previous Thread | Next Thread

1 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Generating XMLfile using shellscript

I have a text file in the following form and i want to store it in 2-d array. After that i have to generate a XMLfile using the elements of array. Please tell me :how store this file in the form of 2-d array and then howto generate XML file using that array...all coding has to be done in bash... (14 Replies)
Discussion started by: cynosure2009
14 Replies
Login or Register to Ask a Question