Beispiel #1
0
  /**
   * This method is called when a PCDATA element is encountered. A Java reader is supplied from
   * which you can read the data. The reader will only read the data of the element. You don't need
   * to check for boundaries. If you don't read the full element, the rest of the data is skipped.
   * You also don't have to care about entities; they are resolved by the parser.
   *
   * @param reader the Java reader from which you can retrieve the data
   * @param systemID the system ID of the XML data source
   * @param lineNr the line in the source where the element starts
   * @throws java.lang.Exception If an exception occurred while processing the event.
   */
  public void addPCData(Reader reader, String systemID, int lineNr) throws Exception {
    int bufSize = 2048;
    int sizeRead = 0;
    StringBuffer str = new StringBuffer(bufSize);
    char[] buf = new char[bufSize];

    for (; ; ) {
      if (sizeRead >= bufSize) {
        bufSize *= 2;
        str.ensureCapacity(bufSize);
      }

      int size = reader.read(buf);

      if (size < 0) {
        break;
      }

      str.append(buf, 0, size);
      sizeRead += size;
    }

    XMLElement elt = new XMLElement(null, systemID, lineNr);
    elt.setContent(str.toString());

    if (!this.stack.empty()) {
      XMLElement top = (XMLElement) this.stack.peek();
      top.addChild(elt);
    }
  }
Beispiel #2
0
  /**
   * This method is called when a new XML element is encountered.
   *
   * @see #endElement
   * @param name the name of the element
   * @param nsPrefix the prefix used to identify the namespace
   * @param nsSystemID the system ID associated with the namespace
   * @param systemID the system ID of the XML data source
   * @param lineNr the line in the source where the element starts
   */
  public void startElement(
      String name, String nsPrefix, String nsSystemID, String systemID, int lineNr) {
    XMLElement elt = new XMLElement(name, systemID, lineNr);

    if (this.stack.empty()) {
      this.root = elt;
    } else {
      XMLElement top = (XMLElement) this.stack.peek();
      top.addChild(elt);
    }

    this.stack.push(elt);
  }
  public static void main(String args[]) throws Exception {
    if (args.length == 0) {
      System.err.println("Usage: java DumpXML file.xml");
      Runtime.getRuntime().exit(1);
    }

    IXMLParser parser = XMLParserFactory.createDefaultXMLParser();
    IXMLReader reader = StdXMLReader.fileReader(args[0]);
    parser.setReader(reader);
    XMLElement xml = (XMLElement) parser.parse();

    xml.addChild(null);
    (new XMLWriter(System.out)).write(xml);
  }
 protected void addStatusXML(XMLElement xmlResult, String statusTag) {
   XMLElement xtag = new XMLElement(statusTag);
   xtag.setEmpty();
   xmlResult.addChild(xtag);
 }