List of usage examples for org.w3c.dom Element getTagName
public String getTagName();
From source file:MainClass.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);// w ww. j ava2s .c o m DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("yourFile.xml"); Element rootElement = doc.getDocumentElement(); NodeList children = rootElement.getChildNodes(); Node current = null; for (int i = 0; i < children.getLength(); i++) { current = children.item(i); if (current.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) current; if (element.getTagName().equalsIgnoreCase("tableOfContents")) { rootElement.removeChild(element); } } } System.out.println(doc.getDocumentElement()); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);/*from www .j a v a 2s .co m*/ DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("yourFile.xml"); Element rootElement = doc.getDocumentElement(); NodeList children = rootElement.getChildNodes(); Node current = null; int count = children.getLength(); for (int i = 0; i < count; i++) { current = children.item(i); if (current.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) current; if (element.getTagName().equalsIgnoreCase("tableOfContents")) { element.setAttribute("showPageNumbers", "no"); } } } System.out.println(doc.getDocumentElement()); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);//from w ww . j ava 2 s .c om DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("yourFile.xml"); Element rootElement = doc.getDocumentElement(); NodeList children = rootElement.getChildNodes(); Node current = null; int count = children.getLength(); for (int i = 0; i < count; i++) { current = children.item(i); if (current.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) current; if (element.getTagName().equalsIgnoreCase("tableOfContents")) { // Get the list of <tocEntry> items NodeList tocitems = element.getElementsByTagName("tocEntry"); // Obtain a reference to the second one Node secondChild = tocitems.item(1); // Create a new <tocEntry> element Element newTOCItem = doc.createElement("tocEntry"); // Create a new "Help" text node Text newText = doc.createTextNode("Help"); // Make it a child of the new <tocEntry> element // <tocEntry>Help</tocEntry> newTOCItem.appendChild(newText); // Add the new <tocEntry> element to <tableOfContents> element.insertBefore(newTOCItem, secondChild); } } } System.out.println(doc.getDocumentElement()); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new File("testrss.xml")); Element root = doc.getDocumentElement(); if (!root.getTagName().equalsIgnoreCase("rss")) { throw new IOException("Invalid RSS document"); }/* w w w .j a v a 2 s .c o m*/ List<RSSChannel> channels = readChannels(root.getChildNodes()); for (RSSChannel channel : channels) { System.out.println("Channel: "); System.out.println(" title: " + channel.getTitle()); System.out.println(" link: " + channel.getLink()); System.out.println(" description: " + channel.getDescription()); for (RSSItem item : channel.getItems()) { System.out.println(" Item: "); System.out.println(" title: " + item.getTitle()); System.out.println(" link: " + item.getLink()); System.out.println(" description: " + item.getDescription()); System.out.println(" pubDate: " + item.getPubDate()); System.out.println(" guid: " + item.getGuid()); } } }
From source file:marytts.tools.analysis.CopySynthesis.java
/** * @param args/*from w ww. j a va2 s . c om*/ */ public static void main(String[] args) throws Exception { String wavFilename = null; String labFilename = null; String pitchFilename = null; String textFilename = null; String locale = System.getProperty("locale"); if (locale == null) { throw new IllegalArgumentException("No locale given (-Dlocale=...)"); } for (String arg : args) { if (arg.endsWith(".txt")) textFilename = arg; else if (arg.endsWith(".wav")) wavFilename = arg; else if (arg.endsWith(".ptc")) pitchFilename = arg; else if (arg.endsWith(".lab")) labFilename = arg; else throw new IllegalArgumentException("Don't know how to treat argument: " + arg); } // The intonation contour double[] contour = null; double frameShiftTime = -1; if (pitchFilename == null) { // need to create pitch contour from wav file if (wavFilename == null) { throw new IllegalArgumentException("Need either a pitch file or a wav file"); } AudioInputStream ais = AudioSystem.getAudioInputStream(new File(wavFilename)); AudioDoubleDataSource audio = new AudioDoubleDataSource(ais); PitchFileHeader params = new PitchFileHeader(); params.fs = (int) ais.getFormat().getSampleRate(); F0TrackerAutocorrelationHeuristic tracker = new F0TrackerAutocorrelationHeuristic(params); tracker.pitchAnalyze(audio); frameShiftTime = tracker.getSkipSizeInSeconds(); contour = tracker.getF0Contour(); } else { // have a pitch file -- ignore any wav file PitchReaderWriter f0rw = new PitchReaderWriter(pitchFilename); if (f0rw.contour == null) { throw new NullPointerException("Cannot read f0 contour from " + pitchFilename); } contour = f0rw.contour; frameShiftTime = f0rw.header.skipSizeInSeconds; } assert contour != null; assert frameShiftTime > 0; // The ALLOPHONES data and labels if (labFilename == null) { throw new IllegalArgumentException("No label file given"); } if (textFilename == null) { throw new IllegalArgumentException("No text file given"); } MaryTranscriptionAligner aligner = new MaryTranscriptionAligner(); aligner.SetEnsureInitialBoundary(false); String labels = MaryTranscriptionAligner.readLabelFile(aligner.getEntrySeparator(), aligner.getEnsureInitialBoundary(), labFilename); MaryHttpClient mary = new MaryHttpClient(); String text = FileUtils.readFileToString(new File(textFilename), "ASCII"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); mary.process(text, "TEXT", "ALLOPHONES", locale, null, null, baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); docFactory.setNamespaceAware(true); DocumentBuilder builder = docFactory.newDocumentBuilder(); Document doc = builder.parse(bais); aligner.alignXmlTranscriptions(doc, labels); assert doc != null; // durations double[] endTimes = new LabelfileDoubleDataSource(new File(labFilename)).getAllData(); assert endTimes.length == labels.split(Pattern.quote(aligner.getEntrySeparator())).length; // Now add durations and f0 targets to document double prevEnd = 0; NodeIterator ni = MaryDomUtils.createNodeIterator(doc, MaryXML.PHONE, MaryXML.BOUNDARY); for (int i = 0; i < endTimes.length; i++) { Element e = (Element) ni.nextNode(); if (e == null) throw new IllegalStateException("More durations than elements -- this should not happen!"); double durInSeconds = endTimes[i] - prevEnd; int durInMillis = (int) (1000 * durInSeconds); if (e.getTagName().equals(MaryXML.PHONE)) { e.setAttribute("d", String.valueOf(durInMillis)); e.setAttribute("end", new Formatter(Locale.US).format("%.3f", endTimes[i]).toString()); // f0 targets at beginning, mid, and end of phone StringBuilder f0String = new StringBuilder(); double startF0 = getF0(contour, frameShiftTime, prevEnd); if (startF0 != 0 && !Double.isNaN(startF0)) { f0String.append("(0,").append((int) startF0).append(")"); } double midF0 = getF0(contour, frameShiftTime, prevEnd + 0.5 * durInSeconds); if (midF0 != 0 && !Double.isNaN(midF0)) { f0String.append("(50,").append((int) midF0).append(")"); } double endF0 = getF0(contour, frameShiftTime, endTimes[i]); if (endF0 != 0 && !Double.isNaN(endF0)) { f0String.append("(100,").append((int) endF0).append(")"); } if (f0String.length() > 0) { e.setAttribute("f0", f0String.toString()); } } else { // boundary e.setAttribute("duration", String.valueOf(durInMillis)); } prevEnd = endTimes[i]; } if (ni.nextNode() != null) { throw new IllegalStateException("More elements than durations -- this should not happen!"); } // TODO: add pitch values String acoustparams = DomUtils.document2String(doc); System.out.println("ACOUSTPARAMS:"); System.out.println(acoustparams); }
From source file:Main.java
/** * Returns true if the element's containing document * @param elem//from w w w. j av a2 s . c om * @return */ public static boolean isInDitaDocument(Element elem) { Element root = elem.getOwnerDocument().getDocumentElement(); Element cand = root; if (cand.getTagName().equals(DITA_ELEM_TAGNAME)) { // FIXME: This a little weak but unlikely to return false positives. return true; } return root.hasAttributeNS(DITA_ARCH_NS, DITA_ARCH_VERSION_ATTNAME); }
From source file:Main.java
private static void validateTagName(Element n, String expected) throws IOException { if (!n.getTagName().equalsIgnoreCase(expected)) { throw new IOException("Expected tag name '" + expected + "' but was '" + n.getTagName() + "'."); }/* ww w . j a v a 2s . c o m*/ }
From source file:Main.java
/** * Returns the first node that is a direct child of root with the coresponding * name. Does not search children of children. *//*from www. java 2 s . co m*/ public static Element getFirstChild(Element root, String name) { NodeList nl = root.getChildNodes(); int size = nl.getLength(); for (int i = 0; i < size; i++) { Node node = nl.item(i); if (!(node instanceof Element)) continue; Element ele = (Element) node; if (ele.getTagName().equals(name)) return ele; } return null; }
From source file:Main.java
/** * get single element by tag name//from w ww. j av a2 s. c o m * @param element * @param tag * @return */ public static List<Element> getElementsByTagName(Element element, String tag) { List<Element> elems = new ArrayList<Element>(); NodeList list = element.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); if (node instanceof Element) { Element e = (Element) node; if (e.getTagName().equals(tag)) { elems.add(e); } } } return elems; }
From source file:Main.java
private static String getLocalName(Element elem) { return (elem.getNamespaceURI() == null) ? elem.getTagName() : elem.getLocalName(); }