1

標準のJava DOMパーサーを使用してxmlファイルを処理しています-

それを処理し、xml ドキュメントに変更を加えたので、変更された xml を表示したいと思います。変更された XML を文字列変数に格納するにはどうすればよいですか。

参考までに、私が使用しているコードを以下に示します--

        str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><config><var-def name=\"initial_no\">3972971</var-def><var-def name=\"webpage\">asdf</var-def><cloudwhile condition=\"${i.toInt() != 500}\" index=\"i\" returnvalue=\"false\" indexstart=\"1\" upperbound=\"500\"><var-def name=\"webpage\" overwrite=\"true\"><cloudwhile condition=\"${i.toInt() != 500}\" index=\"i\" returnvalue=\"false\" indexstart=\"100\" upperbound=\"700\"></cloudwhile><try><body><http url=\"www.google.com/patents/US${initial_no.toInt()+i.toInt()}\"/></body><catch>ERROR- No content found for this patent number.</catch></try></var-def><file action=\"write\" path=\"data/${initial_no.toInt()+i.toInt()}_content.html\"><var name=\"webpage\"/></file></cloudwhile></config>";
        InputStream is = new ByteArrayInputStream(str.getBytes("UTF-8"));
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(is);
                    //some processing..
4

2 に答える 2

1

ドキュメントを操作したら、Transformer を使用してドキュメントを出力できます。最初の子に属性を追加する例を次に示します。

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.xml.sax.SAXException;

public class XmlExample {

    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, TransformerException {
        String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><config><var-def name=\"initial_no\">3972971</var-def><var-def name=\"webpage\">asdf</var-def><cloudwhile condition=\"${i.toInt() != 500}\" index=\"i\" returnvalue=\"false\" indexstart=\"1\" upperbound=\"500\"><var-def name=\"webpage\" overwrite=\"true\"><cloudwhile condition=\"${i.toInt() != 500}\" index=\"i\" returnvalue=\"false\" indexstart=\"100\" upperbound=\"700\"></cloudwhile><try><body><http url=\"www.google.com/patents/US${initial_no.toInt()+i.toInt()}\"/></body><catch>ERROR- No content found for this patent number.</catch></try></var-def><file action=\"write\" path=\"data/${initial_no.toInt()+i.toInt()}_content.html\"><var name=\"webpage\"/></file></cloudwhile></config>";
        InputStream is = new ByteArrayInputStream(str.getBytes("UTF-8"));
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(is);

        // Example processing:  Add an attribute to first child
        NamedNodeMap attributes = doc.getFirstChild().getAttributes();
        Attr attribute = doc.createAttribute("joes-attribute");
        attribute.setValue("joes-value");
        attributes.setNamedItem(attribute);

        // Use a Transformer to output to whatever you want.
        TransformerFactory tFactory =
          TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();

        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(System.out);
        transformer.transform(source, result);
    }
}

詳細については、http: //www.petefreitag.com/item/445.cfmをご覧ください。

于 2012-06-11T18:11:05.450 に答える
1

Transformer API を使用して、必要なことを行うことができます。

import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.OutputKeys;
import javax.xml.transform.stream.StreamResult;

class SaveDom 
{
    public static void main(String[] args) throws Exception
    {
        String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><config><var-def name=\"initial_no\">3972971</var-def><var-def name=\"webpage\">asdf</var-def><cloudwhile condition=\"${i.toInt() != 500}\" index=\"i\" returnvalue=\"false\" indexstart=\"1\" upperbound=\"500\"><var-def name=\"webpage\" overwrite=\"true\"><cloudwhile condition=\"${i.toInt() != 500}\" index=\"i\" returnvalue=\"false\" indexstart=\"100\" upperbound=\"700\"></cloudwhile><try><body><http url=\"www.google.com/patents/US${initial_no.toInt()+i.toInt()}\"/></body><catch>ERROR- No content found for this patent number.</catch></try></var-def><file action=\"write\" path=\"data/${initial_no.toInt()+i.toInt()}_content.html\"><var name=\"webpage\"/></file></cloudwhile></config>";
        InputStream is = new ByteArrayInputStream(str.getBytes("UTF-8"));
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(is);

        // Write out the xml file
        // Use a Transformer for output
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
        transformer.setOutputProperty("encoding", "UTF-8");

        DOMSource source = new DOMSource(doc);
        java.io.StringWriter sw = new java.io.StringWriter();
        StreamResult _result = new StreamResult(sw);
        transformer.transform(source, _result);
        String out = sw.toString();
        System.out.println(out);
    }
}
于 2012-06-11T18:08:18.260 に答える