示例#1
0
  /**
   * Loads an XML document using the supplied string.
   *
   * @param strXML A string containing the XML string to load into this XML document object This
   *     string can contain an entire XML document or a well-formed fragment.
   * @return true if the load succeeded; false if the load failed
   */
  public boolean jsxFunction_loadXML(final String strXML) {
    try {
      final WebWindow webWindow = getWindow().getWebWindow();

      // determine the charset of the page
      String charset = TextUtil.DEFAULT_CHARSET;
      final SgmlPage sgmlPage = (SgmlPage) webWindow.getEnclosedPage();
      if (sgmlPage != null) {
        final String contentCharset = sgmlPage.getWebResponse().getContentCharset();
        if (contentCharset != null) {
          charset = contentCharset;
        }
      }

      // build a dummy WebResponse
      List<NameValuePair> headers = Collections.emptyList();
      final WebResponseData data =
          new WebResponseData(
              TextUtil.stringToByteArray(strXML, charset), HttpStatus.SC_OK, null, headers);
      final URL hackUrl =
          new URL("http://-htmlunit-internal/XMLDocument.loadXML"); // hack! better solution?
      final WebResponse webResponse = new WebResponse(data, hackUrl, (HttpMethod) null, 0);
      webResponse.getWebRequest().setCharset(charset);

      final XmlPage page = new XmlPage(webResponse, webWindow);
      setDomNode(page);
      return true;
    } catch (final IOException e) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("Error parsing XML\n" + strXML, e);
      }
      return false;
    }
  }
示例#2
0
  /**
   * Loads an XML document using the supplied string.
   *
   * @param strXML A string containing the XML string to load into this XML document object This
   *     string can contain an entire XML document or a well-formed fragment.
   * @return true if the load succeeded; false if the load failed
   */
  @JsxFunction({@WebBrowser(IE), @WebBrowser(CHROME)})
  public boolean loadXML(final String strXML) {
    try {
      final WebWindow webWindow = getWindow().getWebWindow();

      final WebResponse webResponse =
          new StringWebResponse(strXML, webWindow.getEnclosedPage().getUrl());

      final XmlPage page = new XmlPage(webResponse, webWindow);
      setDomNode(page);
      return true;
    } catch (final IOException e) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("Error parsing XML\n" + strXML, e);
      }
      return false;
    }
  }