在Java中制作规范形式的XML文件最简单的方法是什么?你有完成的代码吗?我在网上找到了几个链接,比如这个,这个,这个,但我不能让它起作用:/
谢谢,
伊万
编辑:我使用了那里提出的规范化,但我得到了奇怪的结果.为了更加精确,这个方法不会删除元素之间的空格......这就是我得到的:
<Metric xmlns="http://www.ibm.com/wsla" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="total_memory_consumption_metric" type="double" unit="Mbit" xsi:schemaLocation="http://www.ibm.com/wsla WSLA.xsd"> <Source>ServiceProvider</Source> <MeasurementDirective resultType="double" xsi:type="StatusRequest"> <RequestURI> ***unused*** </RequestURI> </MeasurementDirective> </Metric>
Run Code Online (Sandbox Code Playgroud) 我想用Java中的XSLT转换XML.为此,我正在使用该javax.xml.transform包.但是,我得到了例外javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet.这是我正在使用的代码:
public static String transform(String XML, String XSLTRule) throws TransformerException {
Source xmlInput = new StreamSource(XML);
Source xslInput = new StreamSource(XSLTRule);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(xslInput); // this is the line that throws the exception
Result result = new StreamResult();
transformer.transform(xmlInput, result);
return result.toString();
}
Run Code Online (Sandbox Code Playgroud)
请注意,我标记了抛出异常的行.
当我输入方法时,值XSLTRule是这样的:
<xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:msxsl='urn:schemas-microsoft-com:xslt'
exclude-result-prefixes='msxsl'
xmlns:ns='http://www.ibm.com/wsla'>
<xsl:strip-space elements='*'/>
<xsl:output method='xml' indent='yes'/>
<xsl:template match='@* | node()'>
<xsl:copy>
<xsl:apply-templates select='@* | node()'/> …Run Code Online (Sandbox Code Playgroud) 我正在生成许多随机数,我需要一个好的函数,因为这没有多大帮助:
public static class Randomizer
{
static Random random = new Random((int)DateTime.Now.Ticks);
public static int RandomInteger(int minimum, int maximum)
{
return random.Next(minimum, maximum + 1);
}
public static double RandomDouble()
{
return random.NextDouble();
}
}
Run Code Online (Sandbox Code Playgroud)
当我使用这门课时,我的数字通常都是一样的.你有什么简单的想法可以提高随机数发生器的性能吗?
谢谢,伊万
我想创建一个JavaScript,将每个链接放入属性target="_blank",以便链接将在新选项卡中打开.我就这样做了:
function open_links_in_new_tabs() {
var links = document.documentElement.getElementsByTagName( "a" );
for(var link in links) {
link.setAttribute("target", "_blank");
}
}
window.onload = function() { open_links_in_new_tabs(); }
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用.你知道错误在哪里吗?
谢谢,
伊万