Boa tarde amigos,
Estou com um pequeno problema.
Seguinte, tenho um Parser, que pega informações de um arquivo XML. Tenho que pegar essas informações e acrescentar a elas quando forem lidas uma URL.
Por exemplo: http://www.guj.com/
O problema que estou enfrentando é conseguir fazer com que essa URL seja passada via parâmetro, e não sei mesmo como fazer.
Ai conversei com um colega meu e ele falou em Command line args and print usage. Isso me ajudaria em alguma coisa? Vou postar o código, fica mais fácil pra entender.import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
// TBD: Javadoc
public class WSDLHandler extends DefaultHandler
{
static Logger logger = Logger.getLogger(WSDLHandler.class);
private final String TAG = "wsdl";
public WSDLHandler() throws ParserConfigurationException, SAXException,
IOException
{
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser parser = null;
parser = spf.newSAXParser();
parser.parse("build.xml", this);
BasicConfigurator.configure();
logger.setLevel(Level.ALL);
}
public void startDocument() throws SAXException
{
// TBD: logging
BasicConfigurator.configure();
logger.debug("<<< Start XML File Reading >>>\n");
}
public void startElement(String uri, String localName, String tag,
Attributes atts) throws SAXException
{
// Add an extra if that checks if the tag name matches with the one
// that contains the wsdl attribute
for (int i = 0; i < atts.getLength(); i++)
{
tag = atts.getValue(i);
if (tag.contains(TAG))
{
if (atts.getQName(i).equalsIgnoreCase("wsdl"))
{
String[] nome = atts.getValue(i).split("/");
if (url.endsWith("/")){
System.out.println(url+nome[nome.length - 1]); // AQUI VAI A URL CONCATENADA A INFORMAÇÃO RETIRADA DO
} else { // ARQUIVO, CASO A URL JÁ TENHA "/"
System.out.println(url+"/"+nome[nome.length - 1]); // AQUI TAMBÉM, CASO A URL NÃO TENHA "/"
}
}
}
}
}
public void endDocument() throws SAXException
{
// TBD: logging
logger.info("<<< End XML File Reading >>>");
}
}
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
public class ParserMain
{
public static void main(String[] args) throws ParserConfigurationException,
SAXException, IOException
{
new WSDLHandler();
}
}
Aguardo uma resposta. E se possível, poderiam me dizer se o código está bom, ou se tem algo pra melhorar?
Obrigado.