Java XML Node Transform getNodeValue(NodeList nodeList)

Here you can find the source of getNodeValue(NodeList nodeList)

Description

get Node Value

License

Open Source License

Declaration

private static String getNodeValue(NodeList nodeList)
            throws TransformerFactoryConfigurationError, TransformerException 

Method Source Code

//package com.java2s;
/*//from  w  w  w .  ja v a2 s .  c  o  m
 * Copyright (c) 2012 European Synchrotron Radiation Facility,
 *                    Diamond Light Source Ltd.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

import java.io.StringWriter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

import org.w3c.dom.NodeList;

public class Main {
    private static final Pattern HEADER_PATTERN = Pattern.compile("^\\<\\?.+\\?\\>(.*)", Pattern.DOTALL);

    private static String getNodeValue(NodeList nodeList)
            throws TransformerFactoryConfigurationError, TransformerException {

        final Transformer serializer = TransformerFactory.newInstance().newTransformer();
        serializer.setURIResolver(null);

        final StringBuilder buf = new StringBuilder();
        for (int i = 0; i < nodeList.getLength(); i++) {
            final StringWriter sw = new StringWriter();
            serializer.transform(new DOMSource(nodeList.item(i)), new StreamResult(sw));
            String xml = sw.toString();
            final Matcher matcher = HEADER_PATTERN.matcher(xml);
            if (matcher.matches()) {
                xml = matcher.group(1);
            }
            buf.append(xml);
            buf.append("\n");
        }

        return buf.toString();
    }
}

Related

  1. copyTreeToResult(Node tree, Result result)
  2. createInputStream(Node node)
  3. createXML(Node doc)
  4. getAsText(Node n)
  5. getHumanReadableXml(final Node node)
  6. getValueOfValueNode(Node n, boolean unescape)
  7. getXML(NodeList childNodes)
  8. nodeToByteArray(Node node)
  9. render(final Node node)