我正在尝试使用java获取基本URL.我在我的代码中使用了jtidy解析器来获取标题.我正在使用jtidy正确获取标题,但我没有从给定的URL获取基本URL.
我有一些URL作为输入:
String s1 = "http://staff.unak.is/andy/GameProgramming0910/new_page_2.htm";
String s2 = "http://www.complex.com/pop-culture/2011/04/10-hottest-women-in-fast-and-furious-movies";
Run Code Online (Sandbox Code Playgroud)
从第一个字符串,我想要获取"http://staff.unak.is/andy/GameProgramming0910/"作为基本URL,并从第二个字符串,我想"http://www.complex.com/"作为基本URL.
我正在使用代码:
URL url = new URL(s1);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream in = conn.getInputStream();
Document doc = new Tidy().parseDOM(in, null);
String titleText = doc.getElementsByTagName("title").item(0).getFirstChild()
.getNodeValue();
Run Code Online (Sandbox Code Playgroud)
我收到了titletext,但请告诉我如何从上面的URL获取基本URL?
我正在尝试使用JTidy来打印由用户生成的格式良好的HTML:
<div class="component-holder ng-binding ng-scope ui-draggable ui-draggable-handle" data-component="cronos-datasource" id="cronos-datasource-817277">
<datasource name="" entity="" key="" endpoint="" rows-per-page="">
<i class="cpn cpn-datasource"></i>
</datasource>
</div>
Run Code Online (Sandbox Code Playgroud)
这是我的配置:
Tidy tidy = new Tidy();
tidy.setXHTML(true);
tidy.setIndentContent(true);
tidy.setPrintBodyOnly(true);
tidy.setTidyMark(false);
tidy.setWraplen(2000);
tidy.setDropProprietaryAttributes(false);
tidy.setDropEmptyParas(false);
tidy.setTrimEmptyElements(false);
Run Code Online (Sandbox Code Playgroud)
但是jTidy正在删除我的AngularJS datasource指令.有没有办法解决这个问题?
我从日志中得到这个:
line 1 column 191 - Error: <datasource> is not recognized!
line 1 column 191 - Warning: discarding unexpected <datasource>
Run Code Online (Sandbox Code Playgroud)
删除tidy.setXHTML(true)或设置为false并添加tidy.setXmlTags(true)实际上解决了这个问题,它开始考虑用户定义的标签,但这不是一个好的解决方案,因为JTidy开始尝试关闭自封闭标签.
<!-- this code -->
<img src="anythig.jpg"/>
<div id="anyid"></div>
<!-- will become -->
<img src="anythig.jpg">
<div id="anyid"></div>
</img>
Run Code Online (Sandbox Code Playgroud)
我需要一个用于文本编辑器的格式化程序.我不能保证我们的用户将定义和使用哪些指令.它必须是适用于任何用户定义指令的通用解决方案
我正在使用JTidy v.r938.我正在使用此代码尝试清理页面...
final Tidy tidy = new Tidy();
tidy.setQuiet(false);
tidy.setShowWarnings(true);
tidy.setShowErrors(0);
tidy.setMakeClean(true);
Document document = tidy.parseDOM(conn.getInputStream(), null);
Run Code Online (Sandbox Code Playgroud)
但是当我解析这个URL - http://www.chicagoreader.com/chicago/EventSearch?narrowByDate=This+Week&eventCategory=93922&keywords=&page=1时,事情并没有得到清理.例如,页面上的META标签就像
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
Run Code Online (Sandbox Code Playgroud)
保持为
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
Run Code Online (Sandbox Code Playgroud)
而不是具有"</ META>"标签或显示为"<META http-equiv ="Content-Type"content ="text/html; 字符集= UTF-8 "/>".我通过将生成的JTidy org.w3c.dom.Document输出为String来确认这一点.
我能做些什么才能让JTidy真正清理页面 - 即使它格式良好?我意识到还有其他工具,但这个问题与使用JTIdy有关.
我正在寻找TagSoup和jTidy库的文档(如果可能的话,正式文档).
我想利用这个库来操纵HTML"tagsoup"文件,其中包括与(HTML,XHTML或者HTML5)标签HTML之间的混合不同的命名空间XML标记.
我已经测试HTMLCleaner,NekoHTML和杰里科,但我不为jTidy和TagSoup找到文档,除了最简单的例子来清除文件.
我需要有关操纵内容,替换标签,提取信息等的文档......
谢谢
注意:测试完所有选项后,我使用了StAX/Woodstox:
我正在尝试使用JTidy(jtidy-r938.jar)来清理输入HTML字符串,但我似乎无法正确获取默认设置.通常,诸如"你好世界"之类的字符串在整理后最终成为"helloworld".我想展示我在这里做的事情,任何指针都会非常感激:
假设这rawHtml是包含输入(真实世界)HTML的String.这就是我正在做的事情:
Tidy tidy = new Tidy();
tidy.setPrintBodyOnly(true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
tidy.parse(new StringReader(rawHtml), ps);
return baos.toString("UTF8");
Run Code Online (Sandbox Code Playgroud)
首先,上述代码看起来有什么根本错误吗?我似乎得到了奇怪的结果.
例如,请考虑以下输入:
<p class="MsoNormal" style="text-autospace:none;"><font color="black"><span style="color:black;">???</span></font><b><font color="#7f0055"><span style="color:#7f0055;font-weight:bold;">private</span></font></b><font color="black"><span style="color:black;"> String parseDescription</span></font><font>
输出是:
<p class="MsoNormal" style="text-autospace:none;"><font color=
"black"><span style="color:black;"> </span></font>
<b><font color="#7F0055"><span style=
"color:#7f0055;font-weight:bold;">private</span></font></b><font
color="black"><span style="color:black;">String
parseDescription</span></font></p>
所以,
"public String parseDescription"变为"publicString parseDescription"
提前致谢!
在使用jTidy(在Android上)时,我遇到了一个非常烦人的问题.我发现jTidy适用于我测试过的每个HTML文档,除了以下内容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<!-- Always force latest IE rendering engine & Chrome Frame
Remove this if you use the .htaccess -->
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>templates</title>
<meta name="description" content="" />
<meta name="author" content="" />
<meta name="viewport" content="width=device-width; initial-scale=1.0" />
<!-- Replace favicon.ico & apple-touch-icon.png in the root of your domain and delete these references -->
<link rel="shortcut icon" href="/favicon.ico" />
<link rel="apple-touch-icon" href="/apple-touch-icon.png" />
</head>
<body>
<div>
<header>
<h1>Page Heading</h1>
</header>
<nav>
<p><a href="/">Home</a></p> …Run Code Online (Sandbox Code Playgroud) 我正在尝试用JTidy进行XHTML DOM解析,这似乎是违反直觉的任务.特别是,有一种解析HTML的方法:
Node Tidy.parse(Reader, Writer)
Run Code Online (Sandbox Code Playgroud)
为了获得该节点的<body />,我认为,我应该使用
Node Node.findBody(TagTable)
Run Code Online (Sandbox Code Playgroud)
我应该在哪里获得该TagTable的实例?(构造函数受到保护,我还没有找到工厂来生产它.)
我使用JTidy 8.0-SNAPSHOT.
我有一个使用Spring Framework的Java servlet容器.使用Spring从JSP生成页面以连接所有内容.发送给用户的结果HTML并不像我想的那样整洁.我想在将HTML发送到客户端浏览器之前将其发送到Tidy.
我将它设置为开发工作并在生产中关闭; 从我的角度来看,这是一个胜利者,因为它会让我更容易维护.
关于如何在Spring中干净利落地工作的建议?
我在java中使用Jtidy解析器.
URL url = new URL("www.yahoo.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream in = conn.getInputStream();
doc = new Tidy().parseDOM(in, null);
Run Code Online (Sandbox Code Playgroud)
当我运行它时,"doc = new Tidy().parseDOM(in,null);" 我收到一些警告如下:
Tidy (vers 4th August 2000) Parsing "InputStream"
line 140 column 5 - Warning: <table> lacks "summary" attribute
InputStream: Doctype given is "-//W3C//DTD XHTML 1.0 Strict//EN"
InputStream: Document content looks like HTML 4.01 Transitional
1 warnings/errors were found!
Run Code Online (Sandbox Code Playgroud)
这些警告会在控制台上自动显示.但我不希望这些警告在运行后显示在我的控制台上
doc = new Tidy().parseDOM(in, null);
Run Code Online (Sandbox Code Playgroud)
请帮助我,如何做到这一点,如何从控制台中删除这些警告.
我正在使用 JTidy 从 HTML 转换为 XHTML,但我在我的 XHTML 文件中发现了这个标签 。我可以阻止它吗?
这是我的代码
//from html to xhtml
try
{
fis = new FileInputStream(htmlFileName);
}
catch (java.io.FileNotFoundException e)
{
System.out.println("File not found: " + htmlFileName);
}
Tidy tidy = new Tidy();
tidy.setShowWarnings(false);
tidy.setXmlTags(false);
tidy.setInputEncoding("UTF-8");
tidy.setOutputEncoding("UTF-8");
tidy.setXHTML(true);//
tidy.setMakeClean(true);
Document xmlDoc = tidy.parseDOM(fis, null);
try
{
tidy.pprint(xmlDoc,new FileOutputStream("c.xhtml"));
}
catch(Exception e)
{
}
Run Code Online (Sandbox Code Playgroud) 我正在运行Apache servicemix 4.5.2.我想安装一个功能,即一个jar文件.我想要的功能是jtidy.
pom依赖是:
<dependency>
<groupId>jtidy</groupId>
<artifactId>jtidy</artifactId>
<version>4aug2000r7-dev</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
而存储库是
http://repo1.maven.org/maven2/jtidy/jtidy/4aug2000r7-dev/jtidy-4aug2000r7-dev.jar
我知道命令功能:安装webconsole,例如但jtidy不在我的功能:列表中.我也尝试过使用addurl命令,但它没有用.(addurl mvn:http://repo1.maven.org/maven2/jtidy/jtidy/4aug2000r7-dev)Karaf文档建议使用Features XML架构添加功能描述符,但遗憾的是链接已损坏.
要知道我做的是下载jtidy.jar并将其复制到我的deploy目录.它有效,但我不认为这是正确的方法.
有人知道如何在servicemix中正确安装jtidy吗?
谢谢!
我一直在玩这段代码,我不确定我做错了什么.
我得到一个url,用JTidy清理它,因为它格式不正确,然后我需要找到一个特定的隐藏输入字段(input
type="hidden" name="mytarget" value="313"),所以我知道name属性中的值.
我把它打印出整个html页面,当它清理它时,我就可以将我正在寻找的内容与文档中的内容进行比较.
我的问题是试图确定找到这个的最佳方式,关于我的位置System.out << it.
def http = new HTTPBuilder( url )
http.request(GET,TEXT) { req ->
response.success = { resp, reader ->
assert resp.status == 200
def tidy = new Tidy()
def node = tidy.parse(reader, System.out)
def doc = tidy.parseDOM(reader, null).documentElement
def nodes = node.last.last
nodes.each{System.out << it}
}
response.failure = { resp -> println resp.statusLine }
}
Run Code Online (Sandbox Code Playgroud) 如何在Java中更改标记的HTML内容?例如:
之前:
<html>
<head>
</head>
<body>
<div>text<div>**text**</div>text</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
后:
<html>
<head>
</head>
<body>
<div>text<div>**new text**</div>text</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我尝试了JTidy,但它不支持getTextContent.还有其他解决方案吗?
谢谢,我想解析没有格式良好的HTML.我试过TagSoup,但是当我有这个代码时:
<body>
sometext <div>text</div>
</body>
Run Code Online (Sandbox Code Playgroud)
我希望将"sometext"更改为"someAnotherText",当我使用{bodyNode}.getTextContent()它时,它会给我:"sometext text"; 当我使用setTextContet("someAnotherText"+{bodyNode}.getTextContent())并序列化这些结构时,结果是<body>someAnotherText sometext text</body>,没有<div>标签.这对我来说是个问题.