// Gets the specified XML Schema doc if one is mentioned in the file public static File getSchemaFile(Document xmlDoc) { /** @todo Must be an easier way of doing this... */ logger.logComment("Getting schema file for: " + xmlDoc.getDocumentURI()); NodeList nl = xmlDoc.getChildNodes(); for (int j = 0; j < nl.getLength(); j++) { Node node = nl.item(j); logger.logComment("Type: " + node.getNodeType() + "Name: " + node.getNodeName()); if (node.getNodeName().equals(XML_STYLESHEET_NODE)) { String nodeVal = node.getNodeValue(); logger.logComment("Looking at: " + nodeVal); String xslFileName = nodeVal.substring(nodeVal.indexOf("href=\"") + 6, nodeVal.length() - 1); File xslFile = new File(xslFileName); return xslFile; } if (node.getAttributes().getLength() > 0) { logger.logComment("Attributes: " + node.getAttributes()); if (node.getAttributes().getNamedItem(XML_SCHEMA_LOC_ATTR) != null) { String locString = node.getAttributes().getNamedItem(XML_SCHEMA_LOC_ATTR).getNodeValue(); logger.logComment("Loc string: " + locString); String file = locString.split("\\s")[1]; return new File(file); } } } logger.logError("No node found with name: " + XML_STYLESHEET_NODE); return null; }
private List<Node> getChildNodes(Node parentNode, String tagName) { List<Node> nodeList = new ArrayList<Node>(); for (Node child = parentNode.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() == Node.ELEMENT_NODE && tagName.equals(child.getNodeName())) { nodeList.add(child); } } return nodeList; }
public void read(Document doc) { try { Element root = doc.getDocumentElement(); NodeList nList = doc.getElementsByTagName("user"); for (int i = 0; i < nList.getLength(); i++) { Node node = nList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; } } // Element e1 = (Element) nList.item(0); } catch (Exception e) { } }
/** * Method that reads a XML-file, and returns a Model that contains the information * * @param file * @return * @return */ public static Model readXML(String file) { // initialize table to be filled with content of XML file Model t = new Model(); try { // Create file to be parsed by document parser File xmlfile = new File(file); // create parser DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); // parse the file Document parsedfile = parser.parse(xmlfile); // normalize the parsed file (make it more user-friendly) parsedfile.getDocumentElement().normalize(); NodeList cells = parsedfile.getElementsByTagName("CELL"); for (int i = 0; i < cells.getLength(); i++) { // Get cell at list index i Node currentcell = cells.item(i); // read the elements "location" attributes row/column if (Node.ELEMENT_NODE == currentcell.getNodeType()) { Element cellinfo = (Element) currentcell; // get the row number from node attribute int row = Integer.parseInt(cellinfo.getAttribute("row")) - 1; // get the column number from the node attribute int col = Integer.parseInt(cellinfo.getAttribute("column")) - 1; // get content from node String content = cellinfo.getTextContent(); if (content != null) { content = content.replace("\n", ""); } // Make the content an Integer (if it is a number), easier // for // using it later on // put content in table, with row/column inserted as x/y t.setContent(row, col, (String) content); } } } catch (ParserConfigurationException e) { System.out.println("Fileparser could not be made"); } catch (IOException f) { System.out.println("File could not be parsed, did you enter the correct file name?"); } catch (SAXException g) { System.out.println("Something went wrong in parsing the file"); } return t; }
private void call(String script) throws ParserConfigurationException, TransformerConfigurationException { File callerScript = this.currentScript; Document doc = null; try { this.currentScript = new File(script); DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); doc = db.parse(this.currentScript); } catch (Exception ex) { this.currentScript = callerScript; Utils.onError(new Error.FileParse(script)); return; } NodeList operations = doc.getDocumentElement().getChildNodes(); for (int i = 0; i < operations.getLength(); i++) { Node operation = operations.item(i); if (operation.getNodeType() != Node.ELEMENT_NODE) continue; call(operation); } this.currentScript = callerScript; }