将节点放在节点中

时间:2016-01-31 15:54:09

标签: java xml nodes

我正在努力实现以下目标:

<EMOTable1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.europe.eu/tad.xsd">
  <RED>
    <ALE>B00009</ALE>
  </RED>

我到目前为止已经写了以下内容,但是我未能将ALE置于RED中,其值为B00009

到目前为止我得到了以下内容:

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.newDocument();
    Element rootElement = document.createElement("EMOTable1");


    rootElement.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    rootElement.setAttribute("xmlns", "http://www.europe.eu/tad.xsd");
    document.appendChild(rootElement);//append the root element to the doc

    Element REI = document.createElement("RED");//create a new element
        rootElement.appendChild(REI);//append this new element called REI
        Node node = document.createElement("B00009");

REI.appendChild(node); //将内容放在新元素

1 个答案:

答案 0 :(得分:2)

缺少元素ALE的创建。

因此,您应该创建包含文本B00009的此元素,并添加为元素REI的子元素:

EX:

Element ALE = document.createElement("ALE");
Text codeALE = document.createTextNode("B00009");
ALE.appendChild(codeALE);
REI.appendChield(ALE);