#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Traversing XML data very slow/never ends


    For some reason a command line tool is much quicker at saving data to a file.xml rather than returning data through standard output.

    So it's instructed to save a data.xml file which is then parsed to a Document variable in java. And this happens in a few seconds.

    But then when traversing the Document to put all data in a String it never finish. This is just meant to test traversing. The string result is not needed.
    Code:
        public static String xmlToString(Node xmlDoc) {
            String xmlStr = "\nNode " + xmlDoc.getNodeName() + "\nValue" + xmlDoc.getNodeValue() + "\nText" + xmlDoc.getTextContent() + "\ntoString" + xmlDoc.toString() + "\n";
            
            // Now traverse the rest of the tree in depth-first order.
            if (xmlDoc.hasChildNodes()) {
                // Get the children in a list.
                NodeList nl = xmlDoc.getChildNodes();
                // How many of them?
                int size = nl.getLength();
                for (int i=0; i<size; i++) {
                    // Recursively traverse each of the children.
                    xmlStr += xmlToString(nl.item(i));
                }
            }
            return xmlStr;
        }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Tried altering traverser, but it's still stuck
    Code:
        public static String xmlToString(Node xmlDoc) {
            String xmlStr = "\nNode " + xmlDoc.getNodeName() + "\nText:" + xmlDoc.getTextContent() + "\n";
            if (xmlDoc.hasChildNodes()) {
                xmlStr += xmlToString(xmlDoc.getFirstChild());
            }
            if (xmlDoc.getNextSibling() != null) {
                return xmlStr + xmlToString(xmlDoc.getNextSibling());
            }
            return xmlStr;
        }
  4. #3
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Can you make a small, simple program that compiles, executes and shows the problem?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by NormR
    Can you make a small, simple program that compiles, executes and shows the problem?
    The code is broken up in different files (Netbeans - latest version).

    I'm trying to use the command line tool MP4Box to get information about a media file. With -diso it dumps the file structure in .xml . With -std -diso it returns the data but it takes much longer.

    So this code retrieves xml from file and it takes a few secs:
    Code:
        public String MP4BoxSend(String arg) {
            Runtime rt = Runtime.getRuntime();
            String line, output = "";
            try {
                Process pr = rt.exec("mp4box\\MP4Box.exe " + arg);
                BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
                while ((line = input.readLine()) != null) {
                    output += "\n" + line;
                }
                int exitVal = pr.waitFor();
            } catch(IOException | InterruptedException e) { 
                JOptionPane.showMessageDialog(null, e.toString(), "Error", JOptionPane.ERROR_MESSAGE);
            }
            return output;
        }
    
        Document MediaXml;
        
        public String MP4BoxGetMediaInfo(String file_name) {
            String arg = "-diso \"" + file_name + "\""; // Dump Media Info in XML file
            String Error = MP4BoxSend(arg);
    
            String XmlFilename = ExtendedString.removeExt(file_name) + "_info.xml";
            File file = new File(XmlFilename); // Connect to dumped xml file
            if (file.exists()) {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db;
                try {
                    db = dbf.newDocumentBuilder();
                    MediaXml = db.parse(file);
                } catch (ParserConfigurationException | SAXException | IOException ex) {
                    Logger.getLogger(MP4Box.class.getName()).log(Level.SEVERE, null, ex);
                }
                file.delete();
            }
            return Error;
        }
    And this code calls method that is supposed to traverse xml and output it in string just so I can see traversing works:
    Code:
                MP4Box mb = new MP4Box();
                String MP4BoxError = mb.MP4BoxGetMediaInfo(file_name);
                jTextAreaSource.append("\nXML file parsed! " + MP4BoxError + "\nWill now print out: ");
                jTextAreaSource.append("\n" + ExtendedString.xmlToString(mb.MediaXml));
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    How do I compile and test the code to see the problem? You haven't posted all the code needed for testing.

IMN logo majestic logo threadwatch logo seochat tools logo