📜  GWT XML

📅  最后修改于: 2021-01-02 12:54:30             🧑  作者: Mango

GWT XML

GWT XML(可扩展标记语言)使用自定义标记来描述数据并将数据编码为纯文本。它更加灵活且易于操作。

模块XML格式

模块在XML文件中定义,扩展名应为.gwt.xml 。它位于项目包的根目录中。以下是使用标准项目结构的代码:


    
    
 

解析XML

为了解析XML文本,我们必须首先将原始XML文本解析为XM1 DOM结构。 DOM结构有助于数据导航。 XML解析器位于XMLParser类下。 XMLParser类由parse(String)静态方法组成,该方法被调用以解析XML并返回Document对象。

为了处理解析期间发生的错误(例如,如果XML格式不正确),则XMLParser将抛出DOMException。如果解析成功,则我们收到的Document对象代表内存中的XML文档。

成功解析后,它将创建以下节点:

  • 元素-表示DOM元素,由XML 中的标记指定。
  • 文本-表示元素的开始标记和结束标记之间的文本:此处是一些文本。
  • 注释-表示XML注释:<!-有关此数据的注释->。
  • Attr-表示元素的属性:

实作

以下是包含电子邮件消息的XML代码:



  
2017-03-10T12:03:55Z Re: GWT tutorial
gwt tutorial is being developed.

下面的代码用于从xml提取信息:

private void parseMessage(String messageXml) {
  try {
    // parse the XML document into a DOM
    Document messageDom = XMLParser.parse(messageXml);

    // find the sender's display name in an attribute of the  tag
    Node fromNode = messageDom.getElementsByTagName("from").item(0);
    String from = ((Element)fromNode).getAttribute("displayName");
    fromLabel.setText(from);

    // get the subject using Node's getNodeValue() function
    String subject = messageDom.getElementsByTagName("subject").item(0).getFirstChild().getNodeValue();
    subjectLabel.setText(subject);

    // get the message body by explicitly casting to a Text node
    Text bodyNode = (Text)messageDom.getElementsByTagName("body").item(0).getFirstChild();
    String body = bodyNode.getData();
    bodyLabel.setText(body);

  } catch (DOMException e) {
    Window.alert("Could not parse XML document.");
  }
}

GWT XML构造函数

Constructor Description
XMLTools() It constructs different tools option.

GWT XML通用方法

Method Description
disableIEXMLHackaround() It disables an Internet Explorer-specific work around for the MSXML bug that the ‘xml’ namespace prefix cannot be explicitly declared.
loadWSDL(String wsdlURL, WSDLLoadCallback callback) It loads a WSDL file and create an instance of WebService that allows invoking operations and binding DataSources to web service operations.
loadWSDL(String wsdlURL, WSDLLoadCallback callback, RPCRequest requestProperties) It loads a WSDL file and create an instance of WebService that allows invoking operations and binding DataSources to web service operations.
loadWSDL(String wsdlURL, WSDLLoadCallback callback, RPCRequest requestProperties, boolean autoLoadImports) It loads a WSDL file and create an instance of WebService that allows invoking operations and binding DataSources to web service operations.
loadXMLSchema(String schemaURL, XSDLoadCallback callback) It loads an XML file containing XML schema definitions and create DataSource and SimpleType objects to represent the schema.
loadXMLSchema(String schemaURL, XSDLoadCallback callback, RPCRequest requestProperties) It loads an XML file containing XML schema definitions and create DataSource and SimpleType objects to represent the schema.
nativeXMLAvailable() It returns true if the current browser exposes an XML parser that can be used for Smart GWT XML operations like web service bindings and XML processing.
selectNodes(Object element, String expression) It retrieves a set of nodes from an XML element or document based on an XPath expression.
selectNodes(Object element, String expression, Map namespaces) It retrieves a set of nodes from an XML element or document based on an XPath expression.
toJS(Object elements) It translates an XML fragment to JavaScript collections.

GWT XML上传

在本节中,我们将创建一个带有GWT文件上传和表单面板小部件的上传面板。它允许用户将zip文件上传到服务器。

前端设计

为了通过xml存储信息,我们从两个面板(UploadPanel和DataStorePanel)设计了前端。

DataStorePanel.ui.xml



        
                
                        
                                
                        
                
        

上传面板

在此小部件中,我们创建表单以上传文件,并创建按钮以清除符号列表。

UploadPanel.ui.xml


        
                
                        
                               
                 
                        
                                
                        
                        
                                
                        
                        
                                
                        
                        
                                
                         
                
        


                        
                
                
                        
                                     ....

                        
                
        

上传.java

public UploadPanel() {

                initWidget(uiBinder.createAndBindUi(this));
                uploadForm.setMethod(FormPanel.METHOD_POST);
                uploadForm.setEncoding(FormPanel.ENCODING_MULTIPART);

        }

        @UiHandler("uploadButton")
        void onUploadClick(ClickEvent event) {
                uploadForm.submit();
        }

        @UiHandler("uploadForm")
        void onUploadFormSubmitComplete(SubmitCompleteEvent event) {
                StatusEvent se = new StatusEvent(event.getResults());
                EventBus.get().fireEvent(se);
        }

输出: