我正在尝试解析的文本行,这是 .txt 文件中的一行:
<Index>1</Index><Price>$18.38</Price><Product>Takis Fuego, 1 Ounce (46 Pack)</Product><SoldBy> Sam's Club</SoldBy><href>https://www.google.com/shopping/product/14559077146163012667?hl=en&sxsrf=ALiCzsb1Gfl32-pXt32h0QkkE-T57FtdxA:1670964406762&psb=1&q=takis&prds=eto:15330784880350744537_0,pid:15387764403936018691,rsk:PC_15179594788466925291&sa=X&ved=0ahUKEwjPgJuOvsj-AhUJGlkFHSQsBpcQ8gIIpgooAA</href>
(我正在专门查看价格 xml 标签)
这里是负责提取价格的代码:
price = fileLines.get(i).substring(fileLines.indexOf("<Price>") + "<Price>".length(),fileLines.indexOf("</Price>"));`
这里是被解析行的格式:
String str = "<Index>" + resultNum + "</Index>" + "<Price>" + Price + "</Price>" + "<Product>" + Product + "</Product>" + "<SoldBy> " + Company + "</SoldBy>" + "<href>" + link + "</href>";
代表我正在看的产品。
Here is the stack trace for this line:
java.lang.StringIndexOutOfBoundsException: begin 6, end -1, length 371
at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4601)
at java.base/java.lang.String.substring(String.java:2704)
at SeerWebCrawler.buildGUIFile(SeerWebCrawler.java:134)
at SeerWebCrawler.main(SeerWebCrawler.java:58)
我已将其缩小到认为
</Price><Product>
导致错误但我不确定。任何帮助将不胜感激。
我试过弄乱语法,但我给了我同样的错误。