PHP uses Java analysis XML [original]

xiaoxiao2021-03-06  40

First, if you are not interested in Java or say that your host does not support Java or if you don't plan to learn, please don't waste time here. Please install the Java execution environment and PHP combination. Specific reference http://www.phpx.com/happy/thr78795.html or download http://www.javax.org/download/php_java.rar I wrote Readme.txt Description Documentation, this compression package is a simple example. To download my Java source code, please go to http://www.javax.org/download/javaxml.rar has other questions, please go to http://www.javax.org/. At first I wanted to parse XML with PHP, but I found that PHP should be a bit troublesome, as if PHP5 is convenient, but I have no installation here, or 4.3. Later, I saw an article (http://www-900.cn.ibm.com/index.shtml), I can use Java to do, so I have tried it now. The XML file to be parsed: first.xml, the content is as follows: Xiao Zeng 20 < / AGE> Umbrella 20 in which is the root, is a node, a total of two A Name value name is different, one is a small an umbrella, age.

Let's look at PHP file parsing time: init (); // Here is an initialization, such as the XML file directory in the global.properties file (of course you have downloaded the example) $ javaxml-> parse ("First .xml "); // Specify the file to be parsed, $ javaxml-> get (0) relative to the directory specified in the global.properties file; // This is the first node echo $ javaxml-> getValue (" ")."
"; // get the first node Name tag value echo $ javaxml-> getValue (" age ")."
"; // get the first node AGE tag value $ javaxml -> setValue, "Dad"); // Set the first node Name tag value is big Dad $ javaxml-> get (1); // Here you get the second node echo $ javaxml-> GetValue ("name"). "
"; // get the second node Name tag value Echo $ javaxml-> getValue ("age"). "
"; // get the second node AGE label What is the value?> $ Javaxml-> get (0); get the node location, such as my XML file has two groups , here Get (0) is to take the first group of , get (1) is taken The first output is a small 20 umbrella 20 because $ javaxml-> setvalue ("name", "big head dad"); this modified the value of the Name tag of the first node, the XML file has been It was updated, so when the PHP file was executed once again, the result will become a big head of Dad 20 Umbrella 20 or more simple sentences, the following is my Java class, which uses Jdom to resolve XML.

import org.jdom *; import org.jdom.output *; import org.jdom.input *; import java.io. *; import java.util *; / * Author:.... Umbrellas: 2004/12 / 28 * / public class JavaXml {public String path = null; public String XmlFileName = null; public SAXBuilder sax = null; public Document doc = null; public Element root = null; public List xlist = null; public Element e = null; public Element value = null; public String getTest () {return new String ( "haha");} public JavaXml () {} public String init () {InputStream is = getClass () getResourceAsStream ( "global.properties");. Properties DBProps = new Properties (); try {dbprops.load (IS);} catch ("error file");} this.path = dbprops.getProperty ("xmlpath"); return ("ok" );} public void get (int class) {this.e = (element) xlist.get (child);} public string getValue (String name) {this.Value = E.GetIld (name); return this.Value. getText ();} public void setValue (String name, String value) throws Exception {this.value = e.getChild (name); this.value.setText (value); XMLOutputter xmlout = new XMLOutputter (); xmlout.outp ut (doc, new FileOutputStream (path XmlFileName));} public void Parse (String XmlFileName) throws Exception {this.XmlFileName = XmlFileName; this.sax = new SAXBuilder (); this.doc = sax.build (new FileInputStream ( Path XMLFileName); this.root = doc.getrootElement (); this.xlist = root.getchildren ();}} But when you use it, you don't need to write Java code, as long as you configure the Java execution environment. If you don't understand anything, please keep it, so I don't know if it is more troublesome. Please express your opinion. If you think it can, I will continue to improve the Java class, provide more Analyze the function of XML.

转载请注明原文地址:https://www.9cbs.com/read-75507.html

New Post(0)