把<,>编码?那就不能成為xml的節點了 Text createTextNode(String data) Creates a Text node given the specified string. 你的意思是把这部分插入另外一个完整的树的一个节点里面吗? ->差不多 那先用dom装载它,然后createNode的好像就可以了 ->好像不可以,我不懂如何把它作為Node,實作一下你看可不以可以,可以的話就好了
try XMLFragment: public static DocumentFragment parseXml(Document doc, String fragment) { // Wrap the fragment in an arbitrary element fragment = "<fragment>"+fragment+"</fragment>"; try { // Create a DOM builder and parse the fragment DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document d = factory.newDocumentBuilder().parse( new InputSource(new StringReader(fragment)));
// Import the nodes of the new document into doc so that they // will be compatible with doc Node node = doc.importNode(d.getDocumentElement(), true);
// Create the document fragment node to hold the new nodes DocumentFragment docfrag = doc.createDocumentFragment();
// Move the nodes into the fragment while (node.hasChildNodes()) { docfrag.appendChild(node.removeChild(node.getFirstChild())); }
// Return the fragment return docfrag; } catch (SAXException e) { // A parsing error occurred; the xml input is not valid } catch (ParserConfigurationException e) { } catch (IOException e) { } return null; }
可以顺利解析呀! /* * Created on 2004-9-21 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package weich;import org.jdom.input.SAXBuilder; import org.jdom.Document; import org.jdom.JDOMException; import org.jdom.Element;import java.io.FileInputStream; import java.io.IOException; /** * @author Administrator * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class ParseXml { public static void main(String[] args) { SAXBuilder sb = new SAXBuilder(); Document doc = null; try{ doc = sb.build(new FileInputStream("parse.xml")); }catch(JDOMException jdome){ jdome.printStackTrace(); }catch(IOException ioe){ ioe.printStackTrace(); } Element root = doc.getRootElement(); System.out.println(root.getName()); Element EleName= root.getChild("name"); System.out.println("EleName:"+EleName.getName()+"EleName.getText:"+root.getChildTextTrim("name")); } }
那先用dom装载它,然后createNode的好像就可以了
要解析的文件是不完全符合xml格式的
Text createTextNode(String data)
Creates a Text node given the specified string.
你的意思是把这部分插入另外一个完整的树的一个节点里面吗?
->差不多
那先用dom装载它,然后createNode的好像就可以了
->好像不可以,我不懂如何把它作為Node,實作一下你看可不以可以,可以的話就好了
<name>
aaaaaaa
</name>
</man>
怎么就不能解析啊?
<man>
<name>
aaaaaaa
</name>
</man>
<man>
<name>
bbbbb
</name>
</man>
懂的人給點提示吧
a=new ActiveX.....(Document...);
a.loadXML("<ssss>s.....</ssss>");
a...
public static DocumentFragment parseXml(Document doc, String fragment) {
// Wrap the fragment in an arbitrary element
fragment = "<fragment>"+fragment+"</fragment>";
try {
// Create a DOM builder and parse the fragment
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document d = factory.newDocumentBuilder().parse(
new InputSource(new StringReader(fragment)));
// Import the nodes of the new document into doc so that they
// will be compatible with doc
Node node = doc.importNode(d.getDocumentElement(), true);
// Create the document fragment node to hold the new nodes
DocumentFragment docfrag = doc.createDocumentFragment();
// Move the nodes into the fragment
while (node.hasChildNodes()) {
docfrag.appendChild(node.removeChild(node.getFirstChild()));
}
// Return the fragment
return docfrag;
} catch (SAXException e) {
// A parsing error occurred; the xml input is not valid
} catch (ParserConfigurationException e) {
} catch (IOException e) {
}
return null;
}
/*
* Created on 2004-9-21
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package weich;import org.jdom.input.SAXBuilder;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.Element;import java.io.FileInputStream;
import java.io.IOException;
/**
* @author Administrator
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class ParseXml { public static void main(String[] args) {
SAXBuilder sb = new SAXBuilder();
Document doc = null;
try{
doc = sb.build(new FileInputStream("parse.xml"));
}catch(JDOMException jdome){
jdome.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
}
Element root = doc.getRootElement();
System.out.println(root.getName());
Element EleName= root.getChild("name");
System.out.println("EleName:"+EleName.getName()+"EleName.getText:"+root.getChildTextTrim("name"));
}
}