Java XML Document Parse parseMultipleWorkouts(Document doc)

Here you can find the source of parseMultipleWorkouts(Document doc)

Description

parse Multiple Workouts

License

Open Source License

Declaration

public static Document[] parseMultipleWorkouts(Document doc)
            throws ParserConfigurationException, SAXException, IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import org.w3c.dom.Document;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import java.io.File;

import java.io.IOException;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public class Main {
    private final static String TRAINING_CENTER_DATABASE = "<TrainingCenterDatabase "
            + "xsi:schemaLocation=\"http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2 http://www.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd\" "
            + "xmlns:ns5=\"http://www.garmin.com/xmlschemas/ActivityGoals/v1\" "
            + "xmlns:ns3=\"http://www.garmin.com/xmlschemas/ActivityExtension/v2\" "
            + "xmlns:ns2=\"http://www.garmin.com/xmlschemas/UserProfile/v2\" "
            + "xmlns=\"http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2\" "
            + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:ns4=\"http://www.garmin.com/xmlschemas/ProfileExtension/v1\"></TrainingCenterDatabase>";

    public static Document[] parseMultipleWorkouts(File inFile)
            throws ParserConfigurationException, SAXException, IOException {
        return parseMultipleWorkouts(generateDocument(inFile));
    }/*  www.  ja v  a  2 s.com*/

    public static Document[] parseMultipleWorkouts(Document doc)
            throws ParserConfigurationException, SAXException, IOException {
        NodeList activities = doc.getElementsByTagName("Activity");
        int activitiesLength = activities.getLength();

        if (activitiesLength == 1)
            return new Document[] { doc };
        else {
            Document[] docs = new Document[activitiesLength];

            for (int i = 0; i < activitiesLength; ++i) {
                Node n = activities.item(i);

                Document activity = generateDocument(TRAINING_CENTER_DATABASE);
                Node imported = activity.importNode(n, true);
                activity.getFirstChild().appendChild(imported);

                docs[i] = activity;
            }

            return docs;
        }
    }

    public static Document generateDocument(File file)
            throws ParserConfigurationException, SAXException, IOException {
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
        if ((doc != null) && (doc.getDocumentElement() != null))
            doc.getDocumentElement().normalize();

        return doc;
    }

    public static Document generateDocument(String string)
            throws ParserConfigurationException, SAXException, IOException {
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new InputSource(new StringReader(string)));
        if ((doc != null) && (doc.getDocumentElement() != null))
            doc.getDocumentElement().normalize();

        return doc;
    }
}

Related

  1. parseDocumentIntoStringXML(Document document)
  2. parseDocumentr(File f)
  3. parseFileToDocument(File f)
  4. parseHTMLDocument(String paramString)
  5. parseInitMap(InputStream in, DocumentBuilder builder, Map> initMap)
  6. parseResponse(InputSource is, DocumentBuilder documentBuilder)
  7. parseToDocument(InputStream is, boolean debugModeEnabled)
  8. parseXMLDoc(Element element, Document doc, Map oauthResponse)
  9. parseXmlDocToString(Document xmlDoc)