久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 JAVA 中使用 SAX 解析器從 XML 文件中提取文本節

Extracting Text Nodes From XML File Using SAX Parser in JAVA(在 JAVA 中使用 SAX 解析器從 XML 文件中提取文本節點)
本文介紹了在 JAVA 中使用 SAX 解析器從 XML 文件中提取文本節點的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

所以我目前正在使用 SAX 嘗試從我正在處理的許多 xml 文檔中提取一些信息.到目前為止,提取屬性值真的很容易.但是,我不知道如何從文本節點中提取實際值.

So I am currently using SAX to try and extract some information from a a number of xml documents I am working from. Thus far, it is really easy to extract the attribute values. However, I have no clue how to go about extracting actual values from a text node.

例如,在給定的 XML 文檔中:

For example, in the given XML document:

<w:rStyle w:val="Highlight" /> 
  </w:rPr>
  </w:pPr>
- <w:r>
  <w:t>Text to Extract</w:t> 
  </w:r>
  </w:p>
- <w:p w:rsidR="00B41602" w:rsidRDefault="00B41602" w:rsidP="007C3A42">
- <w:pPr>
  <w:pStyle w:val="Copy" /> 

通過從 val 獲取值,我可以毫無問題地提取突出顯示".但我不知道如何進入該文本節點并退出要提取的文本".

I can extract "Highlight" no problem by getting the value from val. But I have no idea how to get into that text node and get out "Text to Extract".

這是我迄今為止提取屬性值的 Java 代碼...

private static final class SaxHandler extends DefaultHandler 
    {
        // invoked when document-parsing is started:
        public void startDocument() throws SAXException 
        {
            System.out.println("Document processing starting:");
        }

        // notifies about finish of parsing:
        public void endDocument() throws SAXException 
        {
            System.out.println("Document processing finished. 
");
        }

        // we enter to element 'qName':
        public void startElement(String uri, String localName, 
                String qName, Attributes attrs) throws SAXException 
        {
            if(qName.equalsIgnoreCase("Relationships"))
            {
                // do nothing
            }
            else if(qName.equalsIgnoreCase("Relationship"))
            {
                // goes into the element and if the attribute is equal to "Target"...
                String val = attrs.getValue("Target");
                // ...and the value is not null
                if(val != null)
                {
                    // ...and if the value contains "image" in it...
                    if (val.contains("image"))
                    {
                        // ...then get the id value
                        String id = attrs.getValue("Id");
                        // ...and use the substring method to isolate and print out only the image & number
                        int begIndex = val.lastIndexOf("/");
                        int endIndex = val.lastIndexOf(".");
                        System.out.println("Id: " + id + " & Target: " + val.substring(begIndex+1, endIndex));
                    }
                }
            }
            else 
            {
                throw new IllegalArgumentException("Element '" + 
                        qName + "' is not allowed here");
            }
        }

        // we leave element 'qName' without any actions:
        public void endElement(String uri, String localName, String qName) throws SAXException 
        {
            // do nothing;
        }
     }

但我不知道從哪里開始進入該文本節點并提取其中的值.有人有什么想法嗎?

But I have no clue where to start to get into that text node and pull out the values inside. Anyone have some ideas?

推薦答案

下面是一些偽代碼:

private boolean insideElementContainingTextNode;
private StringBuilder textBuilder;

public void startElement(String uri, String localName, String qName, Attributes attrs) {
    if ("w:t".equals(qName)) { // or is it localName?
        insideElementContainingTextNode = true;
        textBuilder = new StringBuilder();
    }
}

public void characters(char[] ch, int start, int length) {
    if (insideElementContainingTextNode) {
        textBuilder.append(ch, start, length);
    }
}

public void endElement(String uri, String localName, String qName) {
    if ("w:t".equals(qName)) { // or is it localName?
        insideElementContainingTextNode = false;
        String theCompleteText = this.textBuilder.toString();
        this.textBuilder = null;
    }
}

這篇關于在 JAVA 中使用 SAX 解析器從 XML 文件中提取文本節點的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Upload progress listener not fired (Google drive API)(上傳進度偵聽器未觸發(Google 驅動器 API))
Save file in specific folder with Google Drive SDK(使用 Google Drive SDK 將文件保存在特定文件夾中)
Google Drive Android API - Invalid DriveId and Null ResourceId(Google Drive Android API - 無效的 DriveId 和 Null ResourceId)
Google drive api services account view uploaded files to google drive using java(谷歌驅動api服務賬戶查看上傳文件到谷歌驅動使用java)
Google Drive service account returns 403 usageLimits(Google Drive 服務帳號返回 403 usageLimits)
com.google.api.client.json.jackson.JacksonFactory; missing in Google Drive example(com.google.api.client.json.jackson.JacksonFactory;Google Drive 示例中缺少)
主站蜘蛛池模板: 天天综合亚洲 | 麻豆成人在线视频 | 91麻豆精品国产91久久久更新资源速度超快 | 亚洲一区二区三区高清 | 中文字幕精品一区 | 人人澡人人射 | 欧美日韩精品久久久免费观看 | 欧美成人免费在线 | 精产国产伦理一二三区 | 一区二区不卡 | av激情在线| 亚洲免费在线视频 | 欧美一区二区三区四区五区无卡码 | 欧美久久一级 | 日本精品视频在线观看 | 国产主播第一页 | 91精品麻豆日日躁夜夜躁 | 久久久123 | 97国产精品视频人人做人人爱 | 久久久久久久久久久一区二区 | 欧美一级全黄 | 午夜欧美一区二区三区在线播放 | 国产精品电影网 | 日韩at| 国产精品久久 | 亚洲欧美中文字幕 | 精品91久久 | 国产精品日韩在线观看 | 精品欧美色视频网站在线观看 | 成人精品一区二区三区中文字幕 | 欧美不卡| 国产在线一区二区三区 | 日韩成人免费视频 | 精品在线观看一区二区 | 日本成年免费网站 | 欧洲性生活视频 | 亚洲二区在线观看 | 久久久噜噜噜久久中文字幕色伊伊 | 日韩免费一区二区 | 久久伊人一区 | 国产精品美女久久久久久免费 |