简体   繁体   English

在Java中返回HTML标记值

[英]Return HTML tag value in Java

I am trying to write java code that will return the value in a HTML tag in java. 我正在尝试编写将在java中的HTML标记中返回值的java代码。 below is the method I been trying to get working.. can someone please help me out 下面是我一直努力工作的方法..有人可以帮帮我

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.seoreport.exceptions.DataNotFoundException;

public class utils {

    public String tagValue(String inHTML, String tag) throws DataNotFoundException
    {
        String value = null;

        String searchFor = "/<" + tag + ">(.*?)<\\/" + tag + "\\>/";

        Pattern pattern = Pattern.compile(searchFor);
        Matcher matcher = pattern.matcher(inHTML);

        return value;

    }

}

why don't yo try to use an XML parser and access to the block using xpath? 为什么不尝试使用XML解析器并使用xpath访问块? you may do something like: 你可以这样做:

// Parse the XML file and build the Document object in RAM
Document doc = docBuilder.parse(new File(fileName));

// Normalise text representation.
// Collapses adjacent text nodes into one node.
doc.getDocumentElement().normalize();

// get tag
xpath = ".//*/"+yourTag;
NodeList content= XPathAPI.selectNodeList(doc, xpath);

doing in this way you will have all the content in the content variable. 通过这种方式,您将获得内容变量中的所有内容。

you can use it as a text using: 你可以使用它作为文本:

content.tostring();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM