Java 在把孩子附在孩子身上时有问题
我试图用Java和org.w3c.dom生成一些简单的XML,但当我尝试将一个子对象附加到一个子对象时,我陷入了困境,类似这样:Java 在把孩子附在孩子身上时有问题,java,xml,dom,Java,Xml,Dom,我试图用Java和org.w3c.dom生成一些简单的XML,但当我尝试将一个子对象附加到一个子对象时,我陷入了困境,类似这样: <root> <child> <childOfTheChild>Some text</childOfTheChild> </child> </root> 我总是得到同样的结果,那就是: <root> <child>null
<root>
<child>
<childOfTheChild>Some text</childOfTheChild>
</child>
</root>
我总是得到同样的结果,那就是:
<root>
<child>null</child>
</root>
无效的
这段代码中是否有问题,或者可能是其他问题
编辑:
打印功能在某些测试XML中正常工作,否则。。。
因此,该功能无需进行一些美容校正:
//Call System.out.println( print(dokument.getFirstChild()) );
private String print(Node node) {
String txt = ""; //xml string presentation
//Get the primary node name and any existing attributes, like <myNode att1='some val'>
if (node.getNodeType() == node.ELEMENT_NODE)
{
//The name
txt += "<" + node.getNodeName();
//Insert the attributes
if (node.hasAttributes())
{
NamedNodeMap atts = node.getAttributes();
for (int i = 0; i < atts.getLength(); i++) {
Node atts = atts.item(i);
txt += " " + atts.getNodeName() + " = '" + atts.getNodeValue() + "'";
}
}
txt += ">\n";
}
int nChilds = -1;
//Get any existing child nodes, so the <root><child1></child1></root>
if (node.hasChildNodes())
{
NodeList childs = node.getChildNodes();
nChilds = childs.getLength();
if (nChilds == 1)
{
txt += childs.item(0).getNodeValue();
}
else
{
for (int j = 0; j < nChilds; j++) {
txt += print(childs.item(j));
}
}
}
//And the ending of the primary node, like </root>
if (node.getNodeType() == node.ELEMENT_NODE)
{
txt += "</" + node.getNodeName();
txt += ">\n";
}
return txt;
}
//调用System.out.println(print(dokument.getFirstChild());
私有字符串打印(节点){
String txt=“”;//xml字符串表示法
//获取主节点名称和任何现有属性,如
if(node.getNodeType()==node.ELEMENT\u node)
{
//名字
txt+=“\n”;
}
int-nChilds=-1;
//获取任何现有子节点,以便
if(node.hasChildNodes())
{
NodeList childs=node.getChildNodes();
nChilds=childs.getLength();
如果(nChilds==1)
{
txt+=childs.item(0.getNodeValue();
}
其他的
{
对于(int j=0;j
对我来说很好吗
import static org.junit.Assert.assertTrue;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.junit.Test;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
public class DomTest {
@Test
public void testDom() throws Exception {
Document document = createEmptyDocument();
Element root = document.getDocumentElement();
Element child = document.createElement("child");
Element childOfTheChild = document.createElement("childOfTheChild");
Text st = document.createTextNode("Some text");
childOfTheChild.appendChild(st);
child.appendChild(childOfTheChild);
root.appendChild(child);
assertTrue(serialise(document).contains("Some text"));
}
private Document createEmptyDocument() throws ParserConfigurationException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DOMImplementation domImpl = dbf.newDocumentBuilder()
.getDOMImplementation();
Document document = domImpl.createDocument(null, "root", null);
return document;
}
private String serialise(Document document)
throws TransformerFactoryConfigurationError,
TransformerConfigurationException, TransformerException {
TransformerFactory xff = TransformerFactory.newInstance();
Transformer xf = xff.newTransformer();
StringWriter sw = new StringWriter();
xf.transform(new DOMSource(document), new StreamResult(sw));
return sw.toString();
}
}
您是如何将您的文档打印到控制台的?打印功能,在回答:)下添加注释。好的,谢谢!似乎我的打印功能有一些错误,将调试:)。它的意思更像dom,这就是为什么它不像序列化。再次感谢!注:使用DOM-LS替代Transformer方法进行序列化。看见
import static org.junit.Assert.assertTrue;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.junit.Test;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
public class DomTest {
@Test
public void testDom() throws Exception {
Document document = createEmptyDocument();
Element root = document.getDocumentElement();
Element child = document.createElement("child");
Element childOfTheChild = document.createElement("childOfTheChild");
Text st = document.createTextNode("Some text");
childOfTheChild.appendChild(st);
child.appendChild(childOfTheChild);
root.appendChild(child);
assertTrue(serialise(document).contains("Some text"));
}
private Document createEmptyDocument() throws ParserConfigurationException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DOMImplementation domImpl = dbf.newDocumentBuilder()
.getDOMImplementation();
Document document = domImpl.createDocument(null, "root", null);
return document;
}
private String serialise(Document document)
throws TransformerFactoryConfigurationError,
TransformerConfigurationException, TransformerException {
TransformerFactory xff = TransformerFactory.newInstance();
Transformer xf = xff.newTransformer();
StringWriter sw = new StringWriter();
xf.transform(new DOMSource(document), new StreamResult(sw));
return sw.toString();
}
}