Dom node operations through XPath in dom4j

Source: Internet
Author: User

This article describes how to read from an XML file, operate the Dom, and output the Dom to a file to show how to operate DOM nodes through XPath in dom4j.

1. Read XML

Document originaltemplate1 = templateutil. getdocument ("com/test/XML/dom4j/templateopr/template1.xml ");
Document originaltemplate2 = templateutil. getdocument ("com/test/XML/dom4j/templateopr/template2.xml ");
Document originaltemplate3 = templateutil. getdocument ("com/test/XML/dom4j/templateopr/template3.xml ");
2. Operate nodes through XPath:

Templateutil. setnodevalue (originaltemplate2, "// M0: T2/M0: e2e/M0: e2edata", "e2e value ");
Templateutil. setnodevalue (originaltemplate2, "// M0: T2/M1: servicestate/M1: statecode", "code value ");

Templateutil. setnodevalue (originaltemplate3, "// M2: T3/m2: serviceaddressing/m2: messageid", "msg id ");
Templateutil. setnodevalue (originaltemplate3, "// M2: T3/m3: cloud/m3: mode", "Code ");


Element t2place = (element) originaltemplate1.selectsinglenode ("// M2: t2place ");
Node t2node = originaltemplate2.selectsinglenode ("// M0: t2 ");

// Add a node to another node as a subnode
T2place. Add (node) t2node. Clone ());

Node t3node = originaltemplate3.selectsinglenode ("// M2: T3 ");
Element t3place = (element) originaltemplate1.selectsinglenode ("// m3: t3place ");
T3place. Add (node) t3node. Clone ());

3. Output xml dom:

Templateutil. createfile (originaltemplate1, "d:/temp/test1.xml ");

Appendix: templateutil class used:

Public class templateutil {
Private Static hashmap <string, document> templatemap = new hashmap <string, document> ();

Private Static Map <string, string> getnamespace (){
Map <string, string> NS = new hashmap <string, string> ();

NS. Put ("M0", "http://www.test.com/m0 ");
NS. Put ("M1", "http://www.test.com/m1 ");
NS. Put ("m2", "http://www.test.com/m2 ");
NS. Put ("m3", "http://www.test.com/m3 ");

Return ns;
}

Public static document getdocument (string tempaltepath, Map <string, string> namespaces) throws extends entexception {
Document document = NULL;
If (templatemap. Get (tempaltepath )! = NULL ){
Document = (document) templatemap. Get (tempaltepath). Clone ();
} Else {
Synchronized (templatemap ){
Document = readrequestfiletemplete (tempaltepath, namespaces );
Templatemap. Put (tempaltepath, document );
Document = (document) document. Clone ();
}
}
Return document;
}

Public static document getdocument (string tempaltepath) throws extends entexception {
Document document = NULL;
If (templatemap. Get (tempaltepath )! = NULL ){
Document = (document) templatemap. Get (tempaltepath). Clone ();
} Else {
Synchronized (templatemap ){
Document = readrequestfiletemplete (tempaltepath, getnamespace ());
Templatemap. Put (tempaltepath, document );
Document = (document) document. Clone ();
}
}
Return document;
}

Private Static document readrequestfiletemplete (string file, Map <string, string> namespaceuris)
Throws into entexception {
Documentfactory = new documentfactory ();
Documentfactory. setxpathnamespaceuris (namespaceuris );
Saxreader reader = new saxreader ();
Reader. setdocumentfactory (documentfactory );

Inputstream isfile = thread. currentthread (). getcontextclassloader (). getresourceasstream (File );
Document Doc = reader. Read (isfile );
Return Doc;
}

Public static void setnodevalue (document node, string nodexpath, string nodevalue ){
Node E = node. selectsinglenode (nodexpath );
If (E! = NULL ){
E. settext (nodevalue = NULL? "": Nodevalue );
}
}

Public static file createfile (document sourcedoc, string sourcefilename) throws filenotfoundexception,
Unsupportedencodingexception, ioexception {

File finalfile = new file (sourcefilename );
Fileoutputstream out = new fileoutputstream (finalfile );
Xmlwriter writer = new xmlwriter (out );
Writer. Write (sourcedoc );
Return finalfile;
}
}

XML

Template1.xml

<Root xmlns: m0 = "http://www.test.com/m0"
Xmlns: M1 = "http://www.test.com/m1"
Xmlns: m2 = "http://www.test.com/m2"
Xmlns: M3 = "http://www.test.com/m3/">
<M0: Template>
<M2: t2place>
</M2: t2place>
<M3: t3place>
</M3: t3place>
</M0: Template>
</Root>

Template2.xml

<Template xmlns: m0 = "http://www.test.com/m0"
Xmlns: M1 = "http://www.test.com/m1/">
<M0: T2>
<M0: e2e>
<M0: e2edata>
E2e data
</M0: e2edata>
</M0: e2e>
<M1: servicestate>
<M1: statecode>
OK
</M1: statecode>
</M1: servicestate>
</M0: T2>
</Template>

 

Template3.xml

<Template xmlns: m2 = "http://www.test.com/m2"
Xmlns: M3 = "http://www.test.com/m3/">
<M2: T3>

<M2: serviceaddressing>
<M2: From>
Http://intra.test.com/
</M2: From>
<M2: To>
<M2: Address>
Http://ccm.intra.test.com
</M2: Address>
</M2: To>
<M2: replyto>
<M2: Address>
Http://intra.test.com/
</M2: Address>
</M2: replyto>
<M2: messageid>
8000588300000
</M2: messageid>
<M2: Action>
Http://ccm.intra.test.com
</M2: Action>
</M2: serviceaddressing>

<M3: cloud>
<M3: Mode>
All_or_nothing
</M3: Mode>
</M3: cloud>
</M2: T3>
</Template>

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.