简体   繁体   中英

Adding namespace prefix XML String using XML DOM

I want to add namespace prefix to all the elements in the XML String using XML DOM. Eg My String is coming in this way:

<root> 
       <operation>test string</operation> 
       <data> 
              <parameter> 
                     <name>test string</name> 
                     <value>test string</value> 
                 </parameter> 
              <parameter> 
                     <name>test string</name> 
                     <value>test string</value> 
                 </parameter> 
              <parameter> 
                     <name>test string</name> 
                     <value>test string</value> 
                 </parameter> 
          </data> 
   </root>

And I want a output XML as:

<ns0:root xmlns:ns0 = "http://www.tibco.com/schemas/BWStatistics-hawk/Schema.xsd2"> 
       <ns0:operation>test string</ns0:operation> 
       <ns0:data> 
              <ns0:parameter> 
                     <ns0:name>test string</ns0:name> 
                     <ns0:value>test string</ns0:value> 
                 </ns0:parameter> 
              <ns0:parameter> 
                     <ns0:name>test string</ns0:ns0:name> 
                     <ns0:value>test string</ns0:value> 
                 </ns0:parameter> 
              <ns0:parameter> 
                     <ns0:name>test string</ns0:name> 
                     <ns0:value>test string</ns0:value> 
                 </ns0:parameter> 
          </ns0:data> 
   </ns0:root>

How can i achieve this optimally in Java??

We can do it with Transformer + SAX. Try this:

    import java.io.StringWriter;

    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.sax.SAXSource;
    import javax.xml.transform.stream.StreamResult;

    import org.xml.sax.Attributes;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;
    import org.xml.sax.XMLReader;
    import org.xml.sax.helpers.XMLFilterImpl;
    import org.xml.sax.helpers.XMLReaderFactory;

    public class Test {

        public static void main(String args[]) throws Exception {
            XMLReader xmlReader = new XMLFilterImpl(XMLReaderFactory.createXMLReader()) {
                String namespace = "http://www.tibco.com/schemas/BWStatistics-hawk/Schema.xsd2";
                String pref = "ns0:";

                @Override
                public void startElement(String uri, String localName, String qName, Attributes atts)
                        throws SAXException {
                    super.startElement(namespace, localName, pref + qName, atts);
                }

                @Override
                public void endElement(String uri, String localName, String qName) throws SAXException {
                    super.endElement(namespace, localName, pref + qName);
                }
            };
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer t = tf.newTransformer();
            StringWriter s = new StringWriter();
            t.transform(new SAXSource(xmlReader, new InputSource("test.xml")), new StreamResult(s));
            System.out.println(s);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM