简体   繁体   English

如何使用java,html或javascript将相对href属性转换为绝对路径?

[英]How can I use java,html or javascript to convert relative href attributes into absolute paths?

I would like to open an html file locally (not fetch it from a server) and somehow ensure that the relative hrefs are turned correctly resolved. 我想在本地打开一个html文件(而不是从服务器获取它),并以某种方式确保正确解决了相对href的问题。 I suppose I need to convert them into absolute paths. 我想我需要将它们转换为绝对路径。

I played around with the html element, in the head of the document, to resolve any relative links on the page, but only had limited success (often the resources could not be found). 我在文档的开头使用了html元素,以解决页面上的任何相对链接,但是成功有限(通常找不到资源)。

I can convert the html source file anyway I wish with java, but ideally I would like to modify the html source as little as possible, for example insert a function that modifies the result of the hrf attribute. 无论如何,我都可以使用java转换html源文件,但理想情况下,我希望尽可能少地修改html源,例如,插入一个修改hrf属性结果的函数。

How would I go about this? 我将如何处理? Do any of the java html parsers do this? 是否有任何Java html解析器可以做到这一点?

Thanks 谢谢

If you want to do it on your own, I recommend you to read the chapter Reference Resolution in RFC 3986 . 如果要自己执行此操作,建议您阅读RFC 3986中的参考分辨率一章。 It explains how to establish the base URI and how to resolve relative URIs by giving an algorithm. 它说明了如何建立基本URI以及如何通过给出算法来解析相对URI。

But there's probably already a library in Java that does that for you. 但是Java中可能已经有一个库可以为您完成此工作。

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

相关问题 如何从Java中的绝对http路径获取相对路径 - How can I get relative path from absolute http paths in Java 相对路径与绝对路径Java - Relative vs Absolute Paths Java 如何从两个绝对路径(或 URL)构造 Java 中的相对路径? - How to construct a relative path in Java from two absolute paths (or URLs)? 如何将绝对路径转换为相对路径,以便可以从img标记引用它? java web - How to convert absolute path to a relative path so that it can be referenced from an img tag? java web 使Struts2 redirectAction使用相对路径而不是绝对路径 - Making Struts2 redirectAction use relative paths instead of absolute paths 将相对路径转换为绝对路径 - Converting Relative Paths to Absolute Paths 将Eclipse Java项目导出到可执行jar时,如何调整相对和绝对路径? - How to adjust relative & absolute paths when exporting eclipse java project to executable jar? 我怎样才能使用相对文件名而不是绝对文件名? - How can I just use the relative file name instead of the absolute file name? 如何在 nashorn javascript 中使用 Paths(java.nio.file.Paths) - How to use Paths(java.nio.file.Paths) in nashorn javascript 我如何将这个JavaScript函数转换为Java - How can i convert this javascript function into java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM