用Java修改XML文件

时间:2017-05-19 08:46:32

标签: java xml

我有以下XML文件

<?xml version="1.0" encoding="UTF-8" >
<devices>
<cryptoDevice>
    <waveform display="MILSTD" name="MILSTD">
        <dataRates>
            <dataRate interleavers="Z,S,L" value="75C"/>
            <dataRate interleavers="Z,S,L" value="150C"/>
            <dataRate interleavers="Z,S,L" value="300C"/>
            <dataRate interleavers="Z,S,L" value="600C"/>
            <dataRate interleavers="Z,S,L" value="1200C"/>
            <dataRate interleavers="Z,S,L" value="2400C"/>  
            <dataRate interleavers="Z,S" value="2K4VC"/>
            <dataRate interleavers="" value="4800U"/>   
        </dataRates>
    </waveform>
    <waveform display="HDR" name="HDR">
        <dataRates>
            <dataRate interleavers="US,VS,S,M,L,VL" value="3200C"/>
            <dataRate interleavers="US,VS,S,M,L,VL" value="4800C"/>
            <dataRate interleavers="US,VS,S,M,L,VL" value="6400C"/>
            <dataRate interleavers="US,VS,S,M,L,VL" value="8000C"/>
            <dataRate interleavers="US,VS,S,M,L,VL" value="9600C"/>
            <dataRate interleavers="" value="12.8KU"/>
        </dataRates>
    </waveform>
 </cryptoDevice>    
 <modem name="ARM-1191">
      <waveform>4</waveform>
       <dataRate>2</dataRate>
       <interleaver>3</interleaver>
</modem>
<modem name="ARM-1192">
    <waveform>10</waveform>
    <dataRate>2</dataRate>
    <interleaver>3</interleaver>
</modem>
<modem name="ETM-1193">
     <waveform>10</waveform>
     <dataRate>2</dataRate>
      <interleaver>3</interleaver>
</modem>
<modem name="ETM-1194">
    <waveform>10</waveform>
    <dataRate>2</dataRate>
     <interleaver>3</interleaver>
</modem></devices>

我想修改名称属性为ARM-1191的调制解调器的波形,dataRate和Interleaver。但我的代码无效。

这是我写的代码:

public class ModifyXmlFile
{

/**
 * @param args
 * @throws ParserConfigurationException
 * @throws IOException
 * @throws SAXException
 * @throws TransformerException
 */
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, TransformerException
{

    // File Path
    String filePath = "Modems.xml";

    // Read XML file.
    File inputFile = new File(filePath);

    // Create DocumentBuilderFactory object.
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

    // Get DocumentBuilder object.
    DocumentBuilder dbuilder = dbFactory.newDocumentBuilder();

    // Parse XML file.
    Document document = dbuilder.parse(inputFile);

    // Get the root element
    Node devices = document.getFirstChild();

    // // Get element by tag name.
    // Node devices = document.getElementsByTagName("devices").item(0);

    // Get devices element list.
    NodeList modemList = devices.getChildNodes();

    // Loop the modem child node

    for (int i = 0; i < modemList.getLength(); i++)
    {
        Node node1 = modemList.item(i);

        Node node = document.getElementsByTagName("modem").item(i);

        NamedNodeMap attr = node.getAttributes();

        Node nodeAttr = attr.getNamedItem("name");

        // if (node.getAttributes().getNamedItem("name").equals("ARM-1191"))
        if (nodeAttr.getNodeValue().equals("ARM-1191"))
        {
            NodeList paramList = node1.getChildNodes();

            for (int j = 0; j < paramList.getLength(); j++)
            {
                Node paramNode = paramList.item(j);

                switch (paramNode.getNodeName())
                {
                case "waveform":
                    paramNode.setTextContent("100");
                    break;
                case "dataRate":
                    paramNode.setTextContent("100");
                    break;
                case "interleaver":
                    paramNode.setTextContent("100");
                    break;
                default:
                    break;
                }

            }
        }

    }

    // save changes into XML file.
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();

    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(new File(filePath));
    transformer.transform(source, result);
    System.out.println("Done");

     }
  }

任何帮助将不胜感激。感谢

更新:我收到以下异常:

Exception in thread "main" java.lang.NullPointerException
at gerard.com.ModifyXmlFile.main(ModifyXmlFile.java:73)

在以下行:

NamedNodeMap attr = node.getAttributes();

2 个答案:

答案 0 :(得分:1)

cryptoDevice也是devices的孩子。因此,在for循环中,您将比modem个节点运行一次。这会导致空指针。

答案 1 :(得分:0)

因此,节点为空。 我想,你需要node1变量

NamedNodeMap attr = node1.getAttributes();