Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Dom集合元素错误_Java_Xml_Dom - Fatal编程技术网

Java Dom集合元素错误

Java Dom集合元素错误,java,xml,dom,Java,Xml,Dom,我正在使用Dom创建xml文件,但我无法编写标记 属性如下 <m:FC_TargetPath="SyndicationUpdated" m:FC_KeepInContent="false" rt:filterable="false"> 例外是 要设置命名空间中的属性,您需要使用setAttributeNS而不是setAttribute,并将相应的命名空间URI传递给它。下面的示例程序: import javax.xml.parsers.DocumentBuilder; import

我正在使用Dom创建xml文件,但我无法编写标记 属性如下

<m:FC_TargetPath="SyndicationUpdated" m:FC_KeepInContent="false" rt:filterable="false">
例外是


要设置命名空间中的属性,您需要使用
setAttributeNS
而不是
setAttribute
,并将相应的命名空间URI传递给它。

下面的示例程序:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Dom
{
   public static void main( String[] args ) throws Throwable
   {
      DocumentBuilderFactory dbf  = DocumentBuilderFactory.newInstance();
      dbf.setNamespaceAware( true );

      DocumentBuilder db = dbf.newDocumentBuilder();
      Document doc = db.newDocument();

      Element root = doc.createElement( "root" );
      root.setAttribute( "xmlns:m" , "http://www.lfinance.fr/blog-rachat-credits" );
      root.setAttribute( "xmlns:rt", "http://www.lfinance.fr/forum-rachat-credits" );
      doc.appendChild( root );

      Element elt = doc.createElement( "simple" );
      elt.setAttribute( "m:FC_TargetPath"   , "false" );
      elt.setAttribute( "m:FC_KeepInContent", "false" );
      elt.setAttribute( "rt:filterable"     , "false" );

      root.appendChild( doc.createTextNode( "\n\t" ));
      root.appendChild( elt );
      root.appendChild( doc.createTextNode( "\n" ));
      TransformerFactory.newInstance().newTransformer().transform(
         new DOMSource( doc ),
         new StreamResult( System.out ));
   }
}
输出:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root
    xmlns:m="http://www.lfinance.fr/blog-rachat-credits"
    xmlns:rt="http://www.lfinance.fr/forum-rachat-credits">
    <simple
        m:FC_KeepInContent="false"
        m:FC_TargetPath="false"
        rt:filterable="false" />
</root>


“我出错了”我们无法确定它是什么。请确保在xml文档中为m和rt声明了名称空间?你得到的错误是什么?反问:你至少试过在互联网上寻找错误吗?
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Dom
{
   public static void main( String[] args ) throws Throwable
   {
      DocumentBuilderFactory dbf  = DocumentBuilderFactory.newInstance();
      dbf.setNamespaceAware( true );

      DocumentBuilder db = dbf.newDocumentBuilder();
      Document doc = db.newDocument();

      Element root = doc.createElement( "root" );
      root.setAttribute( "xmlns:m" , "http://www.lfinance.fr/blog-rachat-credits" );
      root.setAttribute( "xmlns:rt", "http://www.lfinance.fr/forum-rachat-credits" );
      doc.appendChild( root );

      Element elt = doc.createElement( "simple" );
      elt.setAttribute( "m:FC_TargetPath"   , "false" );
      elt.setAttribute( "m:FC_KeepInContent", "false" );
      elt.setAttribute( "rt:filterable"     , "false" );

      root.appendChild( doc.createTextNode( "\n\t" ));
      root.appendChild( elt );
      root.appendChild( doc.createTextNode( "\n" ));
      TransformerFactory.newInstance().newTransformer().transform(
         new DOMSource( doc ),
         new StreamResult( System.out ));
   }
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root
    xmlns:m="http://www.lfinance.fr/blog-rachat-credits"
    xmlns:rt="http://www.lfinance.fr/forum-rachat-credits">
    <simple
        m:FC_KeepInContent="false"
        m:FC_TargetPath="false"
        rt:filterable="false" />
</root>