[jdom-interest] Question about manipulating a document

rolf.lear at algorithmics.com rolf.lear at algorithmics.com
Fri Feb 29 04:17:00 PST 2008


Hi Ramo.

Try the following:

    public static void main(String[] args) throws JDOMException,
IOException {
        String docstr = "<?xml version=\"1.0\"
encoding=\"ISO-8859-1\"?>\n"
            + "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 "
            + "Transitional//EN\" "
            +
"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
            + "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
            + "<body />\n"
            + "</html>\n";

        Document doc = new SAXBuilder().build(new
CharArrayReader(docstr.toCharArray()));

        // Add the php Processing Instruction.....
        doc.addContent(0, new ProcessingInstruction("php",
"phpinfo();"));
        
        XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
        out.output(doc, System.out);
        System.out.flush();

    }

The above code will output:

<?xml version="1.0" encoding="UTF-8"?>
<?php phpinfo();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <body />
</html>

You will notice a few things.....
Firstly, by default, the Pretty Format output will be in UTF-8, not
ISO-8859-1. You will need to modify your code to say something like:
        XMLOutputter out = new
XMLOutputter(Format.getPrettyFormat().setEncoding("ISO-8859-1"));
To get the ISO-8859-1 encoding.

Second thing to notice is that the order of the processing instructions
will not put the php first. The xml processing instructions always comes
first....

Rolf

-----Original Message-----
From: jdom-interest-bounces at jdom.org
[mailto:jdom-interest-bounces at jdom.org] On Behalf Of Ramo At Skuff
Sent: Friday, February 29, 2008 6:00 AM
To: jdom-interest at jdom.org
Subject: [jdom-interest] Question about manipulating a document

Hello,

i have a question about manipulating a document. 
I have a method, something like this:

public void writeDocumentXML()
	{
					
		    try {
			 
			final XMLOutputter output2 = new
XMLOutputter(Format.getPrettyFormat());
		    	    FileOutputStream fod2 = new
FileOutputStream(sqlFile);
			    output2.output(xmlDoc, fod2);
			    fod2.flush();
			    fod2.close();
			} catch (IOException e) {
			    
			    e.printStackTrace();
			}

			
		
	}

The output is something like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html" />.....

How can i manipulat the document, that i get something like this:

<?php phpinfo(); ?>
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html" />

So that the first line oft he document is not the xml-definition but the
php-definition?

Is there a way to handle it?

Many thanks!

Ramo

_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com

 
--------------------------------------------------------------------------
This email and any files transmitted with it are confidential and proprietary to Algorithmics Incorporated and its affiliates ("Algorithmics"). If received in error, use is prohibited. Please destroy, and notify sender. Sender does not waive confidentiality or privilege. Internet communications cannot be guaranteed to be timely, secure, error or virus-free. Algorithmics does not accept liability for any errors or omissions. Any commitment intended to bind Algorithmics must be reduced to writing and signed by an authorized signatory.
--------------------------------------------------------------------------




More information about the jdom-interest mailing list