Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Read in XML file & search for a string

Options
  • 24-02-2005 12:54pm
    #1
    Registered Users Posts: 902 ✭✭✭


    Howdy all, got a quick query....

    I'm designing a GUI that's a bit comlpex to explain but I have the whole thing nearly finished (first project in a new job) and the last functionality of it is to read in an XML file and search for a string.

    Every page I go to in reference to XML and Java go on about the functionality of the two, bla, bla, but what I want is a simple task.....

    I've read numerous pages on reading in this and that but none of them seem to work properly. I know it's probably just a few simple lines of code, maybe my brain's just fried but I can't see it.

    Any help would be greatly appreciated...

    Steve


Comments

  • Registered Users Posts: 568 ✭✭✭phil


    A more detailed explanation would probably be better. XML files contain meta-data which may or may not be relevant for searching.


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    More detail for sure but perhaps you should be looking into XPath (which is used to search XML) and the documentation on whatever XML Parser you're using.


  • Registered Users Posts: 902 ✭✭✭thesteve


    Apologies folks... it's pretty simple...

    A couple of our new servers can only be monitored by sending a soap request which then returns an xml file to a specific directory. A program has been written by one of our developers to send this request that uses java and is executed using a cmd file which holds the ip address of the server and the correct port. If the request is sent and received successfully, the xml file is returned, holding the results of the soap query.

    So say for example I was working in a car dealership with a different server for each manufacturer, if i sent the soap request to the Ford server, the returned xml file would contain somewhere in it the strings, focus, mondeo, etc.... (sorry i can't be more specific, privacy, secrecy, all that ****e but this is a good example)

    Now, I work in the incident management dept so if there was a problem we'd have to execute this cmd file, check the returned xml file and see if that specific server was ok. But we have 34 servers to check and the cmd file would have to be edited to change the ip address, and each xml file checked for each server. This takes time and the idea of our dept is to recover as quickly as possible. Anyway muggins here gets hired out of college, they see I've done 4 modules of java and give me the assignment....

    So what I've done so far is successfully design the GUI so you can choose the 'manufacturer', choose the correct server, and choose the string to be searched. The GUI generates the correct cmd file, executes it successfully, and displays the readout from the command prompt on the GUI (just to show it was sent and received ok).

    The next step is to be run in the background. All I want is to point the app to the directory holding the xml file, search it for the string chosen and return a boolean value if it's there or not.

    I'm sure this is pretty simple, I probably don't have to get into xml parsing or interacting the two platforms as i don't want to display anything in xml, just simply search the file and return true or false....

    Thanks for the help guys and sorry I didnt elaborate in the first post....


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    You'll have to do a degree of parsing to determine the character encoding, but after that it sounds like you can just treat it like a text file in the encoding in question (as long as I understand your requirements correctly).


  • Registered Users Posts: 902 ✭✭✭thesteve


    cheers for the help, had to parse a bit in the end... here it is for reference...

    public String xmlFile2String(String fileName)
    {
    StringWriter sw = new StringWriter();

    try{
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    org.xml.sax.InputSource inputSource = new org.xml.sax.InputSource(fileName);
    Document document = documentBuilderFactory.newDocumentBuilder().parse(inputSource);

    Transformer serializer = TransformerFactory.newInstance().newTransformer();
    serializer.transform(new DOMSource(document), new StreamResult(sw));
    jTextArea2.append("\n");
    jTextArea2.append("string converted");
    }
    catch (Exception e) {
    jTextArea2.append("error on conversion");
    e.printStackTrace();
    }
    jTextArea2.append("string passed back");
    return sw.toString();
    }


  • Advertisement
  • Closed Accounts Posts: 324 ✭✭madramor


    returned file like, from example
    <?xml version='1.0' encoding='UTF-8'?>
    <make>focus</make>
    </xml>

    you don't just parse the file and look for the word focus

    you read in the xml doc, and get the value of the tag <make>
    which will return focus.

    the best way to do this is using SAX simple api for xml
    this is very quick and simple, here a link to example
    http://java.sun.com/j2ee/1.4/docs/tutorial/doc/index.html
    Chapter 5

    take a few days to doit, if anybody asks just say, i'm test different
    xml parsing techniques to find best/most suitable.


Advertisement