package tools.xmltools;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
import org.w3c.dom.CharacterData;
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;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;
import org.xml.sax.SAXException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by IntelliJ IDEA.
* User: Igor
* Date: 18.12.2005
* Time: 15:26:59
* To change this template use File | Settings | File Templates.
*/
public class XMLTools
{
/**
* Вернуть значение первой попавшийся CDATA
* @return строка
*/
public static String getCDATAValue(Node node)
{
if(node==null) return "";
NodeList items = node.getChildNodes();
for (int i=0;i
* <element><childName/></element>
* @param parent
* @param childName
* @return Дочерный элемент либо null, если элементов с таким именем нет
*/
public static Element getChild(Element parent, String childName){
NodeList nl = parent.getChildNodes();
for (int i = 0; i node элементом (instanceOf {@link Element})
* и имеет ли имя "tagname"
* @param node
* @param tagname
* @return
*/
public static boolean isElement(Node node, String tagname){
return node instanceof Element && node.getNodeName().equals(tagname);
}
//вернуть первый попавшийся узел среди дочерних
public static Node getFirstNodeOnName(Node node,String nodename)
{
Node[] mas=new Node[50]; //depth
int pos=0;
mas[pos] = node.getFirstChild();
while (mas[pos] != null)
{
if(mas[pos].getNodeName().equals(nodename))
{
return mas[pos];
}
if(mas[pos].getFirstChild()!=null)
{
pos++;
mas[pos]=mas[pos-1].getFirstChild();
}else
{
//если не идёт дальше пытаемся подняться в верх по дереву
while (true)
{
mas[pos] = mas[pos].getNextSibling();
if (mas[pos]==null)
{
if(pos>0){ pos--; }else{ break; }
}else
{
break;
}
}
}
}
return null;
}
/**
* Return first from childs in first deep level on name
* @param node Find in
* @param nodename Name node
* @return node
*/
public static Node getNodeOnName(Node node,String nodename)
{
if(node==null) return null;
Node nextNode = node.getFirstChild();
while(nextNode != null)
{
if(nextNode.getNodeName().equals(nodename)) return nextNode;
nextNode=nextNode.getNextSibling();
}
return null;
}
//Сериализовать узел в строку
public static String getOuterXML(Node node)
{
DOMImplementationLS domImplementation = (DOMImplementationLS) node.getOwnerDocument().getImplementation();
LSSerializer lsSerializer = domImplementation.createLSSerializer();
if (!(node instanceof Document))
{
lsSerializer.getDomConfig().setParameter("xml-declaration", false);
}
return lsSerializer.writeToString(node);
}
public static Node findFirstNodeOnAttribute(Node node, String nodename,String attribute,String val)
{
Node result=null;
if(node==null) return result;
javax.xml.xpath.XPathFactory xPathfactory = javax.xml.xpath.XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr=null;
Object exprResult=null;
try {
expr = xpath.compile("//*/"+nodename+"[@"+attribute+"='" + val + "']");
exprResult = expr.evaluate(node, XPathConstants.NODESET);
} catch (XPathExpressionException ex) {
}
NodeList nodeList = (NodeList) exprResult;
if (nodeList.getLength() > 0)
result = nodeList.item(0);
return result;
}
/**
* Поиск среди текущего и дочерних узлов
* @param {Node} node Корневой узел
* @param {String} nodename Имя первого попавшегося узла
* @returns {undefined}
*/
public static Node findFirstNode(Node node, String nodename)
{
Node result=null;
if(node==null) return result;
javax.xml.xpath.XPathFactory xPathfactory = javax.xml.xpath.XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr=null;
Object exprResult=null;
try {
expr = xpath.compile("//*/"+nodename);
exprResult = expr.evaluate(node, XPathConstants.NODESET);
} catch (XPathExpressionException ex) {
}
NodeList nodeList = (NodeList) exprResult;
if (nodeList.getLength() > 0)
result = nodeList.item(0);
return result;
}
/**
* Найти узел по имени
* @param node
* @param nodename
* @return
*/
public static Node findFirstNode_v2(Node node, String nodename)
{
if(node==null || nodename==null) return null;
NodeList items = node.getChildNodes();
for (int i=0;i