将前缀添加到XmlObject的所有标签

时间:2018-11-30 08:13:59

标签: java xml dom xmlbeans

我有xml字符串,并且我想通过向该xml的所有标签添加前缀“ peci”来使用此xml字符串构建新的Dom元素。 这是我的代码:

Document Effective_Change=null;                 
factory=XmlObject.Factory.parse(NewElement);
                    String testxml =factory.toString();
                    System.out.println(testxml);
                    DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();  
                    fact.setNamespaceAware(true);
                    DocumentBuilder build; 
                    build = fact.newDocumentBuilder();
                    Effective_Change = build.parse(new InputSource(new StringReader(testxml)));
                    Effective_Change.setPrefix("peci");
                     System.out.println(factory.xmlText());

在tesxml开头包含:

<peci:xml-fragment xmlns:peci="urn:com.url/peci">
  <Derived_Event_Code>xx</Derived_Event_Code>
  <Effective_Moment>2018-07-23T04:20:04</Effective_Moment>
  <Entry_Moment>2018-07-23T04:20:04</Entry_Moment>
  <Person_Identification isUpdated="1">
    <Government_Identifier isDeleted="1">
      <Government_ID>xxxxx</Government_ID>
      <Government_ID_Type>xxxxxx/Government_ID_Type>
      <Issued_Date>xxxxx</Issued_Date>
    </Government_Identifier>
  </Person_Identification>
</peci:xml-fragment>

但我在这行中不服从

Effective_Change.setPrefix("peci");

我收到此错误:

org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
    at org.apache.xerces.dom.NodeImpl.setPrefix(NodeImpl.java:701)

我想要得到的结果是:

<peci:xml-fragment xmlns:peci="urn:com.url/peci">
  <peci:Derived_Event_Code>xx</peci:Derived_Event_Code>
  <peci:Effective_Moment>2018-07-23T04:20:04</peci:Effective_Moment>
  <peci:Entry_Moment>2018-07-23T04:20:04</peci:Entry_Moment>
  <peci:Person_Identification isUpdated="1">
    <peci:Government_Identifier isDeleted="1">
      <peci:Government_ID>xxxxx</peci:Government_ID>
      <peci:Government_ID_Type>xxxxxx</peci:Government_ID_Type>
      <peci:Issued_Date>xxxxx</peci:Issued_Date>
    </peci:Government_Identifier>
  </peci:Person_Identification>
</peci:xml-fragment>

0 个答案:

没有答案