All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.emory.mathcs.nlp.common.util.XMLUtils Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2015, Emory University
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.emory.mathcs.nlp.common.util;

import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @author Jinho D. Choi ({@code [email protected]})
 */
public class XMLUtils
{
	private XMLUtils() {}
	
	static public String getPrettyPrint(Document doc)
	{
		try
		{
			TransformerFactory tf = TransformerFactory.newInstance();
			Transformer transformer = tf.newTransformer();
			transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
			StringWriter writer = new StringWriter();
			transformer.transform(new DOMSource(doc), new StreamResult(writer));
			return writer.getBuffer().toString();			
		}
		catch (Exception e) {e.printStackTrace();}
		
		return null;
	}
	
	static public Element getElementByTagName(Element element, String name, int index)
	{
		NodeList list = element.getElementsByTagName(name);
		return (Element)list.item(index);
	}
	
	static public Element getFirstElementByTagName(Document document, String name)
	{
		return getFirstElement(document.getElementsByTagName(name));
	}
	
	static public Element getFirstElementByTagName(Element element, String name)
	{
		return getFirstElement(element.getElementsByTagName(name));
	}
	
	static private Element getFirstElement(NodeList list)
	{
		return list.getLength() > 0 ? (Element)list.item(0) : null;
	}
	
	static public List getAttributeNodeList(Element element, Pattern name)
	{
		NamedNodeMap nodes = element.getAttributes();
		List attributes = new ArrayList<>();
		int i, size = nodes.getLength();
		Node node;
		
		for (i=0; i getChildElementList(Element root)
	{
		List list = new ArrayList<>();
		NodeList nodes = root.getChildNodes();
		int i, size = nodes.getLength();
		Node node;
		
		for (i=0; i getTrimmedTextContents(Element element, String tagName)
//	{
//		List contents = new ArrayList<>();
//		NodeList list = element.getElementsByTagName(tagName);
//		int i, len = list.getLength();
//		
//		for (i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy