| Цитата(fixxer @ 12.4.2010, 14:10) | | А если зачитать DOM и отобразить его в JTree? |
Это как? Этого я тоже не знаю.
Я вот решил проблему таким примерно образом; т.е. создав свой OutputStream собирающий весь документ в одну String:
| Код | import java.io.IOException; import java.io.OutputStream; import java.nio.charset.Charset; import java.util.ArrayList;
import javax.swing.JFrame; import javax.swing.JTextArea; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document; import org.w3c.dom.Element;
import com.sun.org.apache.xml.internal.serialize.OutputFormat; import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
@SuppressWarnings("serial") public class Temp extends JFrame {
public Temp() throws IOException, ParserConfigurationException {
// Create some document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dbf.newDocumentBuilder(); Document document = builder.newDocument(); Element rootElement = document.createElement("root"); document.appendChild(rootElement); Element subElement = document.createElement("sub1"); subElement.setTextContent("text 1"); rootElement.appendChild(subElement); subElement = document.createElement("sub2"); subElement.setTextContent("text 2"); rootElement.appendChild(subElement);
// Process the document
OutputFormat format = new OutputFormat(document); format.setIndenting(true); format.setEncoding("UTF-8");
OutputStream outputStream = new StringOutputStream(); XMLSerializer serializer = new XMLSerializer(outputStream, format); serializer.serialize(document);
// Show the result
JTextArea textArea = new JTextArea(outputStream.toString()); this.add(textArea);
this.setSize(400, 300); this.setVisible(true);
}
private class StringOutputStream extends OutputStream {
private ArrayList<Byte> bytes = new ArrayList<Byte>();
@Override public void write(int arg0) throws IOException { bytes.add((byte) arg0); }
public String toString() { byte[] byteArr = new byte[bytes.size()]; for (int i = 0; i < bytes.size(); i++) byteArr[i] = bytes.get(i); return new String(byteArr, Charset.availableCharsets().get( Charset.availableCharsets().firstKey())); } }
public static void main(String args[]) { try { new Temp(); return; } catch (IOException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } }
} |
Но стойкое подозрение, что это решение через ...., а если культурно, подозреваю, что изобрел велосипед. |