xmlutilities.java

来自「FuncPlotter is a combined Java applicati」· Java 代码 · 共 450 行 · 第 1/2 页

JAVA
450
字号
    public static Document createDOMDocument( char[]       data,                                              URI          systemId,                                              ErrorHandler errorHandler )        throws AppException    {        return createDOMDocument( new InputSource( new CharArrayReader( data ) ), systemId, errorHandler );    }    //------------------------------------------------------------------    public static Document createDOMDocument( String data )        throws AppException    {        return createDOMDocument( data, null, null );    }    //------------------------------------------------------------------    public static Document createDOMDocument( String       data,                                              URI          systemId,                                              ErrorHandler errorHandler )        throws AppException    {        return createDOMDocument( new InputSource( new StringReader( data ) ), systemId, errorHandler );    }    //------------------------------------------------------------------    public static Document createDOMDocument( InputStream inStream )        throws AppException    {        return createDOMDocument( inStream, null, null );    }    //------------------------------------------------------------------    public static Document createDOMDocument( InputStream  inStream,                                              URI          systemId,                                              ErrorHandler errorHandler )        throws AppException    {        return createDOMDocument( new InputSource( inStream ), systemId, errorHandler );    }    //------------------------------------------------------------------    public static Document createDOMDocument( InputSource  inputSource,                                              URI          systemId,                                              ErrorHandler errorHandler )        throws AppException    {        try        {            if ( systemId != null )                inputSource.setSystemId( systemId.toString( ) );            DocumentBuilder documentBuilder = createDocumentBuilder( errorHandler != null );            if ( errorHandler != null )                documentBuilder.setErrorHandler( errorHandler );            return documentBuilder.parse( inputSource );        }        catch ( SAXException e )        {            throw new AppException( ErrorId.ERROR_PARSING_FILE, e );        }        catch ( IOException e )        {            throw new AppException( ErrorId.ERROR_PARSING_FILE, e );        }    }    //------------------------------------------------------------------    public static String getAttribute( Element element,                                       String  attributeName )    {        return ( element.hasAttribute( attributeName ) ? element.getAttribute( attributeName ) : null );    }    //------------------------------------------------------------------    public static Element findElement( Element element,                                       String  elementPath )    {        return findElement( element, elementPath, -1 );    }    //------------------------------------------------------------------    public static Element findElement( Element element,                                       String  elementPath,                                       int     index )    {        return findElement( null, element, elementPath, index, false );    }    //------------------------------------------------------------------    public static Element findElement( Document document,                                       String   elementPath )    {        return findElement( document, elementPath, -1, false );    }    //------------------------------------------------------------------    public static Element findElement( Document document,                                       String   elementPath,                                       int      index )    {        return findElement( document, elementPath, index, false );    }    //------------------------------------------------------------------    public static Element findElement( Document document,                                       String   elementPath,                                       boolean  create )    {        return findElement( document, elementPath, -1, create );    }    //------------------------------------------------------------------    public static Element findElement( Document document,                                       String   elementPath,                                       int      index,                                       boolean  create )    {        return findElement( document, document.getDocumentElement( ), elementPath, index, create );    }    //------------------------------------------------------------------    public static Element findElement( Document document,                                       Element  element,                                       String   elementPath,                                       int      index,                                       boolean  create )    {        Node node = element;        String[] elementPathComponents = elementPath.split( "\\.", -1 );        int pathIndex = 0;        while ( pathIndex < elementPathComponents.length )        {            String elementName = elementPathComponents[pathIndex];            Node parentNode = node;            NodeList childNodes = node.getChildNodes( );            int nodeIndex = 0;            while ( nodeIndex < childNodes.getLength( ) )            {                node = childNodes.item( nodeIndex );                if ( (node.getNodeType( ) == Node.ELEMENT_NODE) &&                     node.getNodeName( ).equals( elementName ) )                {                    if ( (index < 0) || (pathIndex != elementPathComponents.length - 1) )                        break;                    if ( ((Element)node).hasAttribute( AttrName.INDEX ) )                    {                        try                        {                            int value = Integer.parseInt( ((Element)node).getAttribute( AttrName.INDEX ) );                            if ( value == index )                                break;                        }                        catch ( NumberFormatException e )                        {                            // ignore                        }                    }                }                ++nodeIndex;            }            if ( nodeIndex >= childNodes.getLength( ) )            {                if ( !create )                    break;                parentNode.appendChild( document.createElement( elementName ) );                node = parentNode.getLastChild( );                if ( (index >= 0) && (pathIndex == elementPathComponents.length - 1) )                    ((Element)node).setAttribute( AttrName.INDEX, Integer.toString( index ) );            }            ++pathIndex;        }        return ( (pathIndex < elementPathComponents.length) ? null : (Element)node );    }    //------------------------------------------------------------------    public static void setNamespaceAttribute( Element element )    {        // Set namespace declaration attribute on this element        String namespaceName = element.getNamespaceURI( );        if ( namespaceName != null )        {            Node parent = element.getParentNode( );            if ( (parent == null) || !namespaceName.equals( parent.getNamespaceURI( ) ) )            {                String prefix = element.getPrefix( );                String attrName = (prefix == null) ? AttrName.XMLNS : AttrName.XMLNS + ":" + prefix;                element.setAttribute( attrName, namespaceName );            }        }        // Set namespace declaration attribute on child elements        NodeList nodes = element.getChildNodes( );        for ( int i = 0; i < nodes.getLength( ); ++i )        {            Node node = nodes.item( i );            if ( node.getNodeType( ) == Node.ELEMENT_NODE )                setNamespaceAttribute( (Element)node );        }    }    //------------------------------------------------------------------}//----------------------------------------------------------------------

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?