implementation java,Java DomImplementationLS

I'm looking to create XML Document objects in Java and serialize them to a byte array (prior to sending them across a TCP connection). I currently have code that looks like this:

public byte [] EncapsulateThingy( ThingyType thingy )

{

parser.reset(); // parser is a pre-existing DocumentBuilder object

Document doc = parser.newDocument();

doc.appendChild( doc.createElement("Thingy") );

// ... add nodes to doc to represent thingy

ByteArrayOutputStream outputStream = new ByteArrayOutputStream( 8192 );

//

// Missing: Write doc to outputStream with xml version 1.0 and UTF-8

// encoding.

//

return outputStream.toByteArray();

}

The Sun Java documentation has info on a set of interfaces which seems to start with DomImplementationLS for loading and saving XML, which I could use to fill in the missing piece above handily. But I can't figure out how to create an object which implements DomImplementationLS.

My ultimate goal is to serialize and deserialize very simple objects to XML encoded in byte arrays, so I can transmit them across a network. I am interested in keeping the solution lightweight, so that it can handle a high throughput of messages.

I am interested in alternate solutions, so long as they let me specify the exact XML structure which gets sent.

I of course have to provide the deserialization when these XML messages are consumed, but there is plenty of documentation and toturials available online for reading XML, but not much for writing it.

I would prefer solutions that are included in Java 6 without adding packages.

解决方案

Hope this helps:

import java.io.ByteArrayOutputStream;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.junit.Test;

import org.w3c.dom.DOMImplementation;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.ls.DOMImplementationLS;

import org.w3c.dom.ls.LSOutput;

import org.w3c.dom.ls.LSSerializer;

public class DomLsTest {

@Test

public void testDomLs() throws Exception {

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();

DOMImplementation di = db.getDOMImplementation();

Document d = di.createDocument("", "foo", null);

Element e = d.createElement("bar");

d.getDocumentElement().appendChild(e);

DOMImplementationLS ls = (DOMImplementationLS) di;

LSOutput lso = ls.createLSOutput();

ByteArrayOutputStream baos = new ByteArrayOutputStream();

lso.setByteStream(baos);

LSSerializer lss = ls.createLSSerializer();

lss.write(d, lso);

System.out.println(baos.toString());

}

}

So, for your code, you would need to do something like:

DOMImplementationLS ls = (DOMImplementationLS) parser.getDOMImplementation();

LSOutput lso = ls.createLSOutput();

lso.setByteStream(...);

LSSerializer lss = ls.createSerializer();

lss.write(..., lso);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值