Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取节点的周围文本?_Java_Search_Text_Nutch_Dom Node - Fatal编程技术网

Java 如何获取节点的周围文本?

Java 如何获取节点的周围文本?,java,search,text,nutch,dom-node,Java,Search,Text,Nutch,Dom Node,我在和纳奇玩。我试图写一些东西,其中还包括检测DOM结构中的特定节点,并从节点周围提取文本数据。e、 来自父节点、兄弟节点等的文本。我研究并阅读了一些示例,然后尝试编写一个插件,用于图像节点。一些代码 if("img".equalsIgnoreCase(nodeName) && nodeType == Node.ELEMENT_NODE){ String imageUrl = "No Url"; String altTex

我在和纳奇玩。我试图写一些东西,其中还包括检测DOM结构中的特定节点,并从节点周围提取文本数据。e、 来自父节点、兄弟节点等的文本。我研究并阅读了一些示例,然后尝试编写一个插件,用于图像节点。一些代码

    if("img".equalsIgnoreCase(nodeName) && nodeType == Node.ELEMENT_NODE){
            String imageUrl = "No Url"; 
            String altText = "No Text";
            String imageName = "No Image Name"; //For the sake of simpler code, default values set to
                                                //avoid nullpointerException in findMatches method

            NamedNodeMap attributes = currentNode.getAttributes();
            List<String>ParentNodesText = new ArrayList<String>();
            ParentNodesText = getSurroundingText(currentNode);

            //Analyze the attributes values inside the img node. <img src="xxx" alt="myPic"> 
            for(int i = 0; i < attributes.getLength(); i++){
                Attr attr = (Attr)attributes.item(i);   
                if("src".equalsIgnoreCase(attr.getName())){
                    imageUrl = getImageUrl(base, attr);
                    imageName = getImageName(imageUrl);
                }
                else if("alt".equalsIgnoreCase(attr.getName())){
                    altText = attr.getValue().toLowerCase();
                }
            }

  private List<String> getSurroundingText(Node currentNode){

    List<String> SurroundingText = new ArrayList<String>();
    while(currentNode  != null){
        if(currentNode.getNodeType() == Node.TEXT_NODE){
            String text = currentNode.getNodeValue().trim();
            SurroundingText.add(text.toLowerCase());
        }

        if(currentNode.getPreviousSibling() != null && currentNode.getPreviousSibling().getNodeType() == Node.TEXT_NODE){
            String text = currentNode.getPreviousSibling().getNodeValue().trim();
            SurroundingText.add(text.toLowerCase());
        }
        currentNode = currentNode.getParentNode();
    }   
    return SurroundingText;
}
if(“img”.equalsIgnoreCase(nodeName)&&nodeType==Node.ELEMENT\u Node){
String imageUrl=“无Url”;
String altText=“无文本”;
String imageName=“无图像名称”;//为了简化代码,将默认值设置为
//避免findMatches方法中的nullpointerException
NamedNodeMap attributes=currentNode.getAttributes();
ListParentNodesText=newarraylist();
ParentNodeText=getSurroundingText(当前节点);
//分析img节点内的属性值。
对于(int i=0;i

这似乎无法正常工作。检测到img标记,检索到图像名称和URL,但没有更多帮助。getSurroundingText模块看起来太难看,我尝试了,但无法改进。我不清楚从何处以及如何提取可能与图像相关的文本。请提供帮助?

您在正确的轨道上,在另一方面,请看以下HTML代码示例:

<div>
   <span>test1</span>
   <img src="http://example.com" alt="test image" title="awesome title">
   <span>test2</span>
</div>

测试1
测试2
在您的例子中,我认为问题在于
img
节点的同级节点,例如,您正在寻找直接同级节点,您可能认为在前面的示例中,这些节点可能是
span
节点,但在本例中是一些虚拟文本节点,因此当您请求
img
的同级节点时,您可以'将获取没有实际文本的空节点

如果我们将前面的HTML重写为:
test1test2
,那么
img
的同级节点将是您想要的
span
节点

我假设在前面的示例中,您希望同时获得“text1”和“text2”,在这种情况下,您需要继续移动,直到找到某个
节点。ELEMENT_Node
,然后获取该节点内的文本。一个好的做法是不要获取您找到的任何内容,而是将范围限制为
p
span
div
,以提高准确性