`

XML file paser sample

    博客分类:
  • Java
XML 
阅读更多

//从XML文件中读取信息,并写入fw.

private void processFile(File file, FileWriter fw) throws Exception {
    String sObject = "";
    String dObject = "";
    String sCondition = "";
    String dCondition = "";
    String sCardinality = "";
    String dCardinality = "";
    boolean isSourceFound = false;
   
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(file);
    doc.getDocumentElement().normalize();


    System.out.println("Root element " +
                       doc.getDocumentElement().getNodeName());
    String aoName = doc.getDocumentElement().getAttribute("Name");
    System.out.println(aoName);
    NodeList nodeLst = doc.getElementsByTagName("AssociationEnd");

   
    for (int s = 0; s < nodeLst.getLength(); s++) {
      Node fstNode = nodeLst.item(s);
      if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
        Element fstElmnt = (Element)fstNode;

        String eo = fstElmnt.getAttribute("Name");
        if (isSourceFound) {
          dObject = eo;
        } else {
          sObject = eo;
        }

        String cardinality = fstElmnt.getAttribute("Cardinality");
        if (cardinality.equals("-1")) {
          cardinality = "*";
        }

        if (isSourceFound) {
          dCardinality = cardinality;
        } else {
          sCardinality = cardinality;
        }

        NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("AttrArray");
        Element attElmnt = (Element)fstNmElmntLst.item(0);
        NodeList itemNodes = attElmnt.getElementsByTagName("Item");
        Element itemElmnt = (Element)itemNodes.item(0);
        String condition = itemElmnt.getAttribute("Value");
        if (isSourceFound) {
          dCondition = condition;
        } else {
          sCondition = condition;
        }
        isSourceFound = !isSourceFound;
      }
    }
    String Cardinality = sCardinality + "..." + dCardinality;
    String joinCondion = sCondition + "=" + dCondition;
    String outString =
      aoName + "\t" + sObject + "\t" + dObject + "\t" + Cardinality + "\t" +
      joinCondion + "\t" + "Y" + "\n";

    System.out.println(outString);
    fw.write(outString);
  }

分享到:
评论

相关推荐

    xmlparser.zip_zip

    描述中的"xml file parser sample"提示我们这个压缩包可能包含了一个用于解析XML文件的示例程序。XML解析器是读取和解释XML文档的软件,它能将XML文件的内容转换为程序可以理解和处理的数据结构。 标签"zip"进一步...

    java_xml.rar_java xml_sax_xml_xml ja_操作 xml

    parser.parse(new File("path_to_your_xml_file.xml"), handler); ``` 5. **XML Java API** `javax.xml.transform`包提供了用于转换XML的API,如将DOM转换为字符串,或者使用XSLT转换XML。`javax.xml.validation...

    Parser

    - "sample.cfg" 是一个示例配置文件,可能用于测试Parser的功能,开发者可以通过查看其内容了解Parser应如何正确解析不同类型的配置文件。 - "class_diagramm.dia" 可能是一个类图文件,通常用于描绘类库中的类及其...

    XML轻松学习手册--XML肯定是未来的发展趋势,不论是网页设计师还是网络程序员,都应该及时学习和了解

    XML的parser发展为两类:一种是"非确认类paeser",只检测文档是否遵守XML语法规则,是否用元素标识建立了文档树。另一种是"确认类paeser",它不但检测文档语法,结构树,而且比较解析你使用的元素标识是否遵守了相应...

    使用DOM4j解析XML文件

    Document doc = reader.read(new File("sample.xml")); Element root = doc.getRootElement(); List&lt;Element&gt; elements = root.elements(); for (Element e : elements) { System.out.println(e.getName() + ":...

    jQGrid 3.5和jQuery UI 1.7.2

    jQGrid 3.5 jQuery UI 1.7.2 Lightness theme (get ...Sample static XML file (do not put this one into the 'url', it won't work) Complete JSON.php and XML.php parser mySQL script for rapid demo integration

    YOLOX训练自己的数据集(voc).docx

    parser.add_argument('--xml_path', default='Annotations', type=str, help='input xml label path') parser.add_argument('--txt_path', default='ImageSets', type=str, help='output txt label path') opt = ...

    Chinese Entity Linking Comprehensive

    ./data/2011/training/tac_kbp_2011_chinese_entity_linking_sample_and_training_queries.xml This file is a concatenation of the queries files originally released in LDC2011E46 (sample) and LDC2011E55...

    The Lancaster Corpus of Mandarin Chinese

    The corpus is marked up for text categories, sample file numbers, paragraphs, sentences and tokens. Linguistic annotations undertaken on the corpus include tokenization and part-of-speech tagging. The...

    JDK_1_6 API

    javax.xml.stream javax.xml.stream.events javax.xml.stream.util javax.xml.transform 此包定义了用于处理转换指令,以及执行从源到结果的转换的一般 API。 javax.xml.transform.dom 此包实现特定于 DOM 的转换 ...

    C#基类库大全

    Sample.xml 12.XML操作类 XmlHelper XMLProcess 13.弹出消息类 MessageBox ShowMessageBox 14.导出Excel 操作类 DataToExcel ExcelHelper ExportExcel GridViewExport 15.分词辅助类 SegList 16.汉字转拼音 ...

    Killtest提供 IBM 000-972 资料下载

    Indicates to the parser which events your handler should be notified of. - **答案**:"C. Aids in interpreting the event parameter in an event handling procedure." - **解析**:这些保留字主要用于帮助...

    eac3to V3.17

    * fixed: v2.84 sometimes chose incorrect XML file * fixed: v2.84 sometimes chose wrong m2ts playlist file * fixed: some actions were eventually applied twice when "-2pass" was used * fixed: AAC ...

    C#基类库大全下载--苏飞版

    Sample.xml 11.XML操作类 XmlHelper XMLProcess 12.弹出消息类 MessageBox ShowMessageBox 13.导出Excel 操作类 DataToExcel ExcelHelper ExportExcel GridViewExport 14.分词辅助类 SegList 15.汉字转拼音 ...

    axis2/c linux 安装步骤

    Axis2LogFile /usr/tmp/AxisLog Axis2MaxLogFileSize 200 Axis2LogLevel LOG_LEVEL SetHandler axis2_module ``` - 验证配置正确性,使用`apachectl configtest`命令,如果没有错误信息,则表示配置成功...

    EurekaLog_7.5.0.0_Enterprise

    4)....Added "--el_injectjcl", "--el_createjcl", and "--el_createdbg" command-line options for ecc32/emake to inject JEDI/JCL debug info, create .jdbg file, and create .dbg file (Microsoft debug format...

    js使用小技巧

    Javascript小技巧一箩筐 事件源对象 event.srcElement.tagName event.srcElement.type ... 捕获释放 event.srcElement.setCapture();...event.srcElement.releaseCapture();... 根据鼠标获得元素: document....

    hibernate.properties

    # See the lgpl.txt file in the root directory or &lt;http://www.gnu.org/licenses/lgpl-2.1.html&gt;. # ###################### ### Query Language ### ###################### ## define query language ...

Global site tag (gtag.js) - Google Analytics