📄 xmlwriter.java
字号:
public void writeXmlDeclaration( CharSequence versionStr, CharSequence encodingStr, Standalone standalone ) throws IOException { write( "<?xml" ); if ( versionStr != null ) { write( VERSION_STR ); writeQuoted( versionStr ); } if ( encodingStr != null ) { write( ENCODING_STR ); writeQuoted( encodingStr ); } if ( standalone != Standalone.NONE ) { write( STANDALONE_STR ); writeQuoted( (standalone == Standalone.NO) ? NO_STR : YES_STR ); } write( "?>" ); writeLineSeparator( ); } //------------------------------------------------------------------ public void writeDocumentType( CharSequence documentName, CharSequence systemId, CharSequence publicId ) throws IOException { write( DOCTYPE_STR ); write( documentName ); writeLineSeparator( ); writeSpaces( DOCTYPE_STR.length( ) ); if ( publicId != null ) { write( PUBLIC_STR ); writeQuoted( publicId ); writeLineSeparator( ); writeSpaces( DOCTYPE_STR.length( ) ); } else write( SYSTEM_STR ); writeQuoted( systemId ); write( '>' ); writeLineSeparator( ); } //------------------------------------------------------------------ public void writeProcessingInstruction( CharSequence target, CharSequence data ) throws IOException { write( "<?" ); write( target ); write( ' ' ); write( data ); write( "?>" ); writeLineSeparator( ); } //------------------------------------------------------------------ public void writeElementStart( CharSequence name, int indent, boolean elementNewLine ) throws IOException { writeElementStart( name, null, indent, elementNewLine, false ); } //------------------------------------------------------------------ public void writeElementStart( CharSequence name, List<Attribute> attributes, int indent, boolean elementNewLine, boolean attrNewLine ) throws IOException { writeSpaces( indent ); write( '<' ); write( name ); if ( attributes != null ) writeAttributes( attributes, indent, attrNewLine ? name.length( ) + 2 : 0 ); write( '>' ); if ( elementNewLine ) writeLineSeparator( ); } //------------------------------------------------------------------ public void writeEndTag( CharSequence name ) throws IOException { write( "</" ); write( name ); write( '>' ); } //------------------------------------------------------------------ public void writeElementEnd( CharSequence name, int indent ) throws IOException { if ( indent > 0 ) writeSpaces( indent ); writeEndTag( name ); writeLineSeparator( ); } //------------------------------------------------------------------ public void writeEmptyElement( CharSequence name, List<Attribute> attributes, int indent, boolean attrNewLine ) throws IOException { writeSpaces( indent ); write( '<' ); write( name ); if ( attributes != null ) writeAttributes( attributes, indent, attrNewLine ? name.length( ) + 2 : 0 ); write( "/>" ); writeLineSeparator( ); } //------------------------------------------------------------------ public void writeComment( CharSequence comment, int indent ) throws IOException { writeSpaces( indent ); write( "<!-- " ); write( comment ); write( " -->" ); writeLineSeparator( ); } //------------------------------------------------------------------ public void writeSpaces( int numSpaces ) throws IOException { writeChars( ' ', numSpaces ); } //------------------------------------------------------------------ public void write( char ch ) throws IOException { if ( ch == '\n' ) { outBuffer.append( lineSeparator ); outStream.append( outBuffer ); outBuffer.setLength( 0 ); } else outBuffer.append( ch ); } //------------------------------------------------------------------ public void write( CharSequence charSeq ) throws IOException { for ( int i = 0; i < charSeq.length( ); ++i ) write( charSeq.charAt( i ) ); } //------------------------------------------------------------------ public void writeEscaped( CharSequence charSeq ) throws IOException { write( escape( charSeq ) ); } //------------------------------------------------------------------ public void writeQuoted( CharSequence charSeq ) throws IOException { write( '"' ); write( charSeq ); write( '"' ); } //------------------------------------------------------------------ public void writeLineSeparator( ) throws IOException { write( '\n' ); } //------------------------------------------------------------------ public void writeChars( char ch, int count ) throws IOException { for ( int i = 0; i < count; ++i ) write( ch ); } //------------------------------------------------------------------ public void writeElementNoText( Element element, int indent ) throws IOException { // Create list of attributes List<Attribute> attributes = new ArrayList<Attribute>( ); NamedNodeMap attrs = element.getAttributes( ); for ( int i = 0; i < attrs.getLength( ); ++i ) { Node node = attrs.item( i ); Attribute attribute = new Attribute( node.getNodeName( ), node.getNodeValue( ), true ); if ( node.getNodeName( ).startsWith( AttrName.XMLNS ) ) attributes.add( 0, attribute ); else attributes.add( attribute ); } // Write element if ( element.hasChildNodes( ) ) { // Write element start tag writeElementStart( element.getTagName( ), attributes, indent, true, false ); // Write 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 ) writeElementNoText( (Element)node, indent + INDENT_INCREMENT ); } // Write element end tag writeElementEnd( element.getTagName( ), indent ); } else writeEmptyElement( element.getTagName( ), attributes, indent, false ); } //------------------------------------------------------------------ private void init( Writer writer ) { outStream = writer; outBuffer = new StringBuilder( 256 ); lineSeparator = DEFAULT_LINE_SEPARATOR; } //------------------------------------------------------------------ private void writeAttributes( List<Attribute> attributes, int indent, int offset ) throws IOException { for ( int i = 0; i < attributes.size( ); ++i ) { if ( i == 0 ) write( ' ' ); else { if ( offset > 0 ) { writeLineSeparator( ); writeSpaces( indent ); writeSpaces( offset ); } else write ( ' ' ); } Attribute attr = attributes.get( i ); write( attr.name ); write( '=' ); writeQuoted( attr.value ); } } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance variables//////////////////////////////////////////////////////////////////////// private FileOutputStream fileOutStream; private Writer outStream; private StringBuilder outBuffer; private String lineSeparator;}//----------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -