Results 1 to 13 of 13

Thread: Xml Parsing

  1. #1
    Nomad is offline Member
    Join Date
    Nov 2008
    Posts
    15
    Rep Power
    0

    Default Xml Parsing

    Hi,

    I have a problem with parsing, that is the parse loops forever and i never get the results.

    as you can see from this peace of code the print with line 72 never gets printed because the document is never created because the parse loops forever so why is that, i even made a new BufferedReader the see that the inputstream is not empty so no problem there, i can even put junk instead of an xml file and still it loops, also tried to flush the data no good...
    can anyone point the problem with parseing and inputstream?

    Java Code:
     public String parsingInputStream(InputStream in) throws ParserConfigurationException, SAXException, IOException {
                Document doc = null;
                DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    
                System.out.println("XMLParser class. Line 70. Test.");
                BufferedReader reader  = new BufferedReader(new InputStreamReader(in));
                System.out.println(reader.readLine());
    
                doc = docBuilder.parse(in);//?????????????????????????????????
                System.out.println("XMLParser class. Line 72. Test.");

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    How did you attach xml file into the xml parser.

    Something like this.

    Java Code:
                DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                Document document = builder.parse("files/updateXML.xml");
    Then workaround with the XPath and a Node

  3. #3
    Nomad is offline Member
    Join Date
    Nov 2008
    Posts
    15
    Rep Power
    0

    Default Reply

    I didn't use a file ,I sent it via client
    This is the client

    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PrintStream;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class Main {
        public Main() {
            try {
                Socket socket = new Socket(InetAddress.getLocalHost(), 123);
                System.out.println("1");
                InputStream input = socket.getInputStream();
                OutputStream output = socket.getOutputStream();
    
                PrintStream print = new PrintStream(output, true);//write to socket.
                print.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Personnel><Employee type=\"permanent\"><Name>Seagull</Name><Id>3674</Id><Age>34</Age></Employee></Personnel>");
                System.out.println("2");
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));//read from socket.
                System.out.println(reader.readLine());
    
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    
    
        public static void main(String[] args) {
            new Main();
        }
    }
    This is the run function from a thread class that receives the file on the server side

    Java Code:
        
    public void run() {
            XMLParser xmlParser = new XMLParser();
            try {
                input = m_client_sock.getInputStream();
                output = new PrintStream(m_client_sock.getOutputStream());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            while (true) {
                try {
                    System.out.println("before-> String response = xmlParser.parsingInputStream(input);");
                    String response = xmlParser.parsingInputStream(input);
    And then it calls the function that i posted previously and jams.

  4. #4
    Nomad is offline Member
    Join Date
    Nov 2008
    Posts
    15
    Rep Power
    0

    Default New tactic

    Tried a new tactic

    Java Code:
        doc = docBuilder.parse(br.readLine());
    or
        doc = docBuilder.parse(br.readLine().toString().trim());
    But i keep getting
    C:\Projects\<?xml version="1.0" encoding="UTF-8"?><Employee type="permanent"><\Employee> (The filename, directory name, or volume label syntax is incorrect)

    Also been seeing this error flashing around occasionally on a random project build

    Java Code:
    org.apache.lucene.index.StaleReaderException: IndexReader out of date and no longer valid for delete, undelete, or setNorm operations
    	at org.apache.lucene.index.DirectoryIndexReader.acquireWriteLock(DirectoryIndexReader.java:246)
    	at org.apache.lucene.index.IndexReader.deleteDocument(IndexReader.java:725)
    	at org.apache.lucene.index.FilterIndexReader.doDelete(FilterIndexReader.java:206)
    	at org.apache.lucene.index.IndexReader.deleteDocument(IndexReader.java:727)
    	at org.netbeans.modules.java.source.usages.LuceneIndex.store(LuceneIndex.java:779)
    	at org.netbeans.modules.java.source.usages.BinaryAnalyser.store(BinaryAnalyser.java:415)
    	at org.netbeans.modules.java.source.usages.BinaryAnalyser.finish(BinaryAnalyser.java:224)
    	at org.netbeans.modules.java.source.usages.RepositoryUpdater$CompileWorker.updateBinary(RepositoryUpdater.java:2589)
    	at org.netbeans.modules.java.source.usages.RepositoryUpdater$CompileWorker.access$3500(RepositoryUpdater.java:1257)
    	at org.netbeans.modules.java.source.usages.RepositoryUpdater$CompileWorker$1.run(RepositoryUpdater.java:1549)
    	at org.netbeans.modules.java.source.usages.RepositoryUpdater$CompileWorker$1.run(RepositoryUpdater.java:1300)
    	at org.netbeans.modules.java.source.usages.ClassIndexManager.writeLock(ClassIndexManager.java:99)
    	at org.netbeans.modules.java.source.usages.RepositoryUpdater$CompileWorker.run(RepositoryUpdater.java:1297)
    	at org.netbeans.modules.java.source.usages.RepositoryUpdater$CompileWorker.run(RepositoryUpdater.java:1257)
    	at org.netbeans.api.java.source.JavaSource$CompilationJob.run(JavaSource.java:1618)
    	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
    	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    	at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    [catch] at java.lang.Thread.run(Thread.java:619)
    I don't know if they are connected but i suspect either incorrect xml syntex or a problem with multithreading is keeping this thing parser from working...

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    I don't think you can read a line-by-line using the XMLParser like this. Either you have to take the complete doc(output from client) in to a buffer and feed it into the parser.

  6. #6
    Nomad is offline Member
    Join Date
    Nov 2008
    Posts
    15
    Rep Power
    0

    Default

    I can't read it into a string because it doesn't save the line for some reason, previously i tried to pass the inputstream into it so what do you propose to do, can you give an example?

  7. #7
    Nomad is offline Member
    Join Date
    Nov 2008
    Posts
    15
    Rep Power
    0

    Default

    Solved(for now).

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Nomad View Post
    Solved(for now).
    Nice, but better to send it here that how you solve it. Others can helpful your comments here who are looking into this thread.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Nomad View Post
    I can't read it into a string because it doesn't save the line for some reason, previously i tried to pass the inputstream into it so what do you propose to do, can you give an example?
    What you mean by doesn't save the line? As you tried input stream is possible to do this. However I'm not clear what you mean by impossible to save?

  10. #10
    Nomad is offline Member
    Join Date
    Nov 2008
    Posts
    15
    Rep Power
    0

    Default

    Hi,
    what i mean is if i do
    String str = reader.readline();
    is that line the str will be the xml file
    but in the next line the str in null...

    I'l post my solution as soon as i figure out what exactly fixed it i think it's the printwriter i used for the server output stream
    now the server works perfectly but the client is experiencing closed socket exception when i try to readline from the socket input.

  11. #11
    Nomad is offline Member
    Join Date
    Nov 2008
    Posts
    15
    Rep Power
    0

    Default

    I used dom parsing (that's what i needed) , the parse function gets inputstream or inputsource, first porblem was an infinite loop and was caused because in order for the parse function to finish you need to close the socket from the client side and then the server does everything ok but the client gets socket closed... ,so my final solution was to read the xml document from the input stream into a string and then i create a new inputsource from the string and give it to the parser and it all works just fine...
    Ok here's what i got:
    (btw my xml document was one long line that's why i only need one readline.)
    Java Code:
            public String parsingInputStream(InputStream in) throws ParserConfigurationException, SAXException, IOException {
                DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    
                BufferedReader reader  = new BufferedReader(new InputStreamReader(in));
    
                String str = reader.readLine();
    
                Document doc = docBuilder.parse(new InputSource(new StringReader(str)));
    Last edited by Nomad; 02-21-2009 at 11:59 PM.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    You are reading a line at a time from the server, right? In that case what happen if the string not contain a valid XML format to the dom?

    I'm confusing on that your socket exception here, how it's related to this? You say that you can't read the output from the server, on client side socket closed?

  13. #13
    Nomad is offline Member
    Join Date
    Nov 2008
    Posts
    15
    Rep Power
    0

    Default

    Youre mixing it up, i'm reading one line at a time but my xml doc is just one long liine and i trim it so no problem there, the code i posted was the server side parsing function ,thats not a question thats the solution the client side socket close was an earlier problem caused by closing the socket outputstream after send the xml doc that was a mistake it's fixed now there is no closing on either side until the job is finished.

Similar Threads

  1. xml parsing help
    By tankhardrive in forum XML
    Replies: 2
    Last Post: 02-11-2009, 11:45 PM
  2. xml parsing
    By gaurav65176 in forum XML
    Replies: 5
    Last Post: 11-12-2008, 11:07 AM
  3. Parsing XML File
    By rstar7 in forum XML
    Replies: 1
    Last Post: 10-10-2008, 03:31 AM
  4. Parsing URL in Java
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 08:14 PM
  5. Parsing URL
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-26-2007, 10:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •