java报告html导出xml格式,Java HTML/XML - 如何输出XML数据作为输入格式

import java.io.IOException;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Attr;

import org.w3c.dom.CDATASection;

import org.w3c.dom.Comment;

import org.w3c.dom.Document;

import org.w3c.dom.DocumentType;

import org.w3c.dom.Element;

import org.w3c.dom.NamedNodeMap;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.w3c.dom.ProcessingInstruction;

import org.w3c.dom.Text;

import org.xml.sax.InputSource;

public class Main {

public static String getXMLData() {

return "";

}

static public void main(String[] arg) {

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

dbf.setValidating(true);

dbf.setNamespaceAware(true);

dbf.setIgnoringElementContentWhitespace(true);

Document doc = null;

try {

DocumentBuilder builder = dbf.newDocumentBuilder();

InputSource is = new InputSource(new StringReader(getXMLData()));

doc = builder.parse(is);

write(doc);

} catch (Exception e) {

System.err.println(e);

}

}

private static final String TAB = " ";

private static void write(Document doc) throws IOException {

outputHeading(doc);

outputElement(doc.getDocumentElement(), "");

}

private static void outputHeading(Document doc) {

System.out.print("<?xml version=\"1.0\"");

DocumentType doctype = doc.getDoctype();

if (doctype != null) {

if ((doctype.getPublicId() == null) && (doctype.getSystemId() == null)) {

System.out.println(" standalone=\"yes\"?>");

} else {

System.out.println(" standalone=\"no\"?>");

}

} else {

System.out.println("?>");

}

}

private static void outputElement(Element node, String indent) {

System.out.print(indent + "

NamedNodeMap nm = node.getAttributes();

for (int i = 0; i < nm.getLength(); i++) {

Attr attr = (Attr) nm.item(i);

System.out.print(" " + attr.getName() + "=\"" + attr.getValue() + "\"");

}

System.out.print(">");

NodeList list = node.getChildNodes();

for (int i = 0; i < list.getLength(); i++)

outputloop(list.item(i), indent + TAB);

System.out.println(indent + "" + node.getTagName()+">");

}

private static void outputText(Text node, String indent) {

System.out.println(indent + node.getData());

}

private static void outputCDATASection(CDATASection node, String indent) {

System.out.println(indent + node.getData());

}

private static void outputComment(Comment node, String indent) {

System.out.println(indent + "");

}

private static void outputProcessingInstructionNode(

ProcessingInstruction node, String indent) {

System.out.println(indent + "" + node.getTarget() + " " + node.getData()

+ "?>");

}

private static void outputloop(Node node, String indent) {

switch (node.getNodeType()) {

case Node.ELEMENT_NODE:

outputElement((Element) node, indent);

break;

case Node.TEXT_NODE:

outputText((Text) node, indent);

break;

case Node.CDATA_SECTION_NODE:

outputCDATASection((CDATASection) node, indent);

break;

case Node.COMMENT_NODE:

outputComment((Comment) node, indent);

break;

case Node.PROCESSING_INSTRUCTION_NODE:

outputProcessingInstructionNode((ProcessingInstruction) node, indent);

break;

default:

System.out.println("Unknown node type: " + node.getNodeType());

break;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值